Free SQL Server Learning - Making the most out of SQL Server Agent
solving sql server problems for millions of dbas and developers since 2006



SQL Server DBA Tips SQL Server Developer Tips SQL Server Business Intelligence Tips SQL Server Career Tips SQL Server Tip Categories SQL Server Tutorials SQL Server Webcasts SQL Server Whitepapers SQL Server Tools SQL Server Questions and Answers MSSQLTips Authors About MSSQLTips SQL Server User Groups SQL Server Events I am MSSQLTips MSSQLTips Advertising Options

MSSQLTips Facebook Page MSSQLTips LinkedIn Page MSSQLTips RSS Feed MSSQLTips Pinterest Page MSSQLTips Twitter Page MSSQLTips Google+ Page














































Using SQL Server meta data to list tables that make up views

By:   |   Read Comments (5)   |   Related Tips: More > Database Administration

Problem
One of the issues I often face is the need to find views that are already established for certain tables. This maybe for other developers, end users or even for myself.  I could search the system tables to find this or explore each view, but are there other ways to easily find a list of all tables that are used for a view or even a list of all views that a table is tied to?

Solution
Generally we can access SQL Server meta data either by using a system stored procedure or through INFORMATION_SCHEMA views. System stored procedure are excellent and optimized for a DBA when using through a query window, but these are not always suitable for application users, because you may be required to further filter the information and thus hinder the performance. So, I decided to get the required information from the INFORMATION_SCHEMA views of SQL Server.

In the below example I retrieve the base table information for view 'vEmployee' in the 'AdventureWorks' database.

Syntax and example to get base tables for a view
-- Syntax to get base tables for a view
USE DBName
GO SELECT view_name, Table_Name FROM INFORMATION_SCHEMA.VIEW_TABLE_USAGE WHERE View_Name = '<giveViewName>' ORDER BY view_name, table_name GO
Syntax to get data for view 'vEmployee'
-- Get base tables for 'vEmployee' in AdventureWorks 
USE AdventureWorks 
GO
SELECT view_name, Table_Name
FROM INFORMATION_SCHEMA.VIEW_TABLE_USAGE
WHERE View_Name = 'vEmployee'
ORDER BY view_name, table_name
GO

The result for the above query shown below and we can now see all of the tables that are part of this view.

If we wanted to look at this from a different angle and find all views that a table is part of we can use the following command:

Syntax to get data for table 'Address'
-- Get views for table 'Address' in AdventureWorks 
USE AdventureWorks 
GO
SELECT view_name, Table_Name
FROM INFORMATION_SCHEMA.VIEW_TABLE_USAGE
WHERE Table_Name= 'Address'
ORDER BY view_name, table_name
GO

The result for the above query shown below and we can now see all views that the Address table is part of.

Meta data views are defined in the INFORMATION_SCHEMA. These views can be found in SQL Server 2000 and newer versions of SQL Server. The INFORMATION_SCHEMA schema is provided in each database of SQL Server. In SSMS you can find views for the 'INFORMATION_SCHEMA' schema in the 'System Views' folder under 'Views' as shown below.

Next Steps

  • Although this is a pretty simple tip, hopefully it will save you some time next time you are looking for which tables make up a certain view.
  • In addition to the above columns that we used, the INFORMATION_SCHEMA.VIEW_TABLE_USAGE view includes the following additional data elements: (VIEW_CATALOG, VIEW_SCHEMA, VIEW_NAME, TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME), so take a look to see how you can use this information.
  • Take the time to explore the other INFORMATION_SCHEMA views to find out what other information is easily exposed for your use.


Last Update: 12/3/2008

About the author

Atif is a passionate SQL Server DBA, technical reviewer and article author.

View all my tips


Print  
Become a paid author


Comments and Feedback:

Thursday, December 04, 2008 - 11:56:12 AM - jerryhung Read The Tip

 Thank you

 easier than select a view in SSMS and View Dependecies, and find the Tables manually


Monday, April 16, 2012 - 6:38:10 PM - MemphisMaven Read The Tip

Thank you so much.  Very helpful!!!


Monday, May 14, 2012 - 12:15:51 AM - Azhar Iqbal Read The Tip

Nice Information.


Thursday, May 31, 2012 - 10:15:56 AM - sai Read The Tip

 

This above code does not list if the view is created from the tables of  a linked server. can you please let me know if you know any way to do this? Thanks


Tuesday, June 05, 2012 - 12:57:36 AM - Atif Read The Tip

@sai. For this scenaio you may union the following statement

SELECT view_name, Table_Name
FROM [LinkedServer].[LinkedDB].INFORMATION_SCHEMA.VIEW_TABLE_USAGE
WHERE View_Name in ('View1','View2')

I hope it would work. If there is any issue then please mention here.

 

 



Post a Comment or Question

Keep it clean and stay on the subject or we may delete your comment.
Your email address is not published. Required fields are marked with an asterisk (*)

*Name   *Email Notify for updates

Signup for our newsletter


Comments
*Enter Code refresh code


 
SQL Monitor
SQL Monitor

Get your priorities straight

with SQL Server Monitoring


Sponsor Information
Find and fix SQL Server problems before they happen - SQL diagnostic manager now with predictive analysis!

SQL Monitor: prioritize your SQL Server workload with easy-to-use performance monitoring

Need SQL Server help and not sure where to turn? Reach out to expert consultants in the USA for a Health Check.

Spring Clean Your Data - Clean your global contact data with Melissa Data tools for SSIS. Download a free trial!

The SQL Server Security THREAT - It’s Closer Than You Think


Copyright (c) 2006-2013 Edgewood Solutions, LLC All rights reserved
privacy | disclaimer | copyright | advertise | about
authors | contribute | feedback | giveaways | user groups
Some names and products listed are the registered trademarks of their respective owners.


Edgewood Solutions LLC | MSSharePointTips.com | MSSQLTips.com