SQL Clone
SQLServerCentral is supported by Redgate
Log in  ::  Register  ::  Not logged in

Get your favorite SSC scripts directly in SSMS with the free SQL Scripts addin. Search for scripts directly from SSMS, and instantly access any saved scripts in your SSC briefcase from the favorites tab.
Download now (direct download link)

View - Table Usage (Recursive)

By Rhett Clement,

Wrote this to analyze the tables that are contained in a view. The problem I had with the INFORMATION_SCHEMA.VIEW_COLUMN_USAGE view is that if one view queries another it reports the view as the Base Table, not the actual database table the data comes from. Considering that this is a legal column definition in a view "1 As Id" then we can see why INFORMATION_SCHEMA doesn't attempt to dig down and tell us where that data came from, since the correct answer could be NULL.

But there are so many times when are views are 'honest' queries between multiple related tables and the occasional view. So if that's what you have this may be a useful way for you to uncover the tables that made up your view, without individually looking at the dependencies visualizer for each view or spending $$$ on software to do that for us. This query attempts to assist in that situation. I say attempts because it is not heavily tested and has limitations as I've hinted at above.

Also includes two other queries that show different summaries of the same data. Intended as an analysis query not something to use in a prodcution app. Requires SQL 2005 or higher. Written on SQL 2005.

I imagine community members will be able to point out additional issues or enhancements. I welcome your feedback.


Total article views: 1055 | Views in the last 30 days: 1
Related Articles

Xquery issue attempting to update an attribute

receiving an error when attempting to add a leading character to a already existing string


DAX Query Basics

In this document I will attempt to talk you through writing your first very simple DAX queries. For ...


How to Refer to A Computed Column in Access-SQL Database Query

Errors created when attempt to use a Computer Column in Query


How to parameterize a TOP values query

There are certain key words in a T-SQL query than cannot be parameterized, including the TOP statem...


Bad Default Database Leads to a Failed Login Attempt

Yesterday we had a user receiving the infamous failed login attempt error (no, that's not a valid IP...