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

By:   |   Comments (9)   |   Related: 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.

table name

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.

results

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.

object explorer
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.


sql server categories

sql server webinars

subscribe to mssqltips

sql server tutorials

sql server white papers

next tip



About the author
MSSQLTips author Atif Shehzad Atif Shehzad is a passionate SQL Server DBA, technical reviewer and article author.

This author pledges the content of this article is based on professional experience and not AI generated.

View all my tips



Comments For This Article




Tuesday, August 7, 2018 - 11:22:38 PM - TyloMan Back To Top (77064)

Good article and very useful. The only issue is that if you use multiple databases on the View the query is only returning the current database.


Wednesday, May 3, 2017 - 4:27:54 AM - CobbNut Back To Top (55405)

Thanks - helpful.  The below will recurse out embedded views. 

 

SELECT a.view_name, a.Table_Name as Level_1, b.TABLE_NAME as Level_2, c.TABLE_NAME as Level_3, d.TABLE_NAME as Level_4, e.TABLE_NAME as Level_5, f.TABLE_NAME as Level_6

, case when f.TABLE_NAME is not null then f.TABLE_NAME

when e.TABLE_NAME is not null then e.TABLE_NAME

when d.TABLE_NAME is not null then d.TABLE_NAME

when c.TABLE_NAME is not null then c.TABLE_NAME

when b.TABLE_NAME is not null then b.TABLE_NAME

when a.TABLE_NAME is not null then a.TABLE_NAME

end as TheTable

, case when f.TABLE_NAME is not null then 6

when e.TABLE_NAME is not null then 5

when d.TABLE_NAME is not null then 4

when c.TABLE_NAME is not null then 3

when b.TABLE_NAME is not null then 2

when a.TABLE_NAME is not null then 1

end as Depth

 

FROM INFORMATION_SCHEMA.VIEW_TABLE_USAGE a

left join INFORMATION_SCHEMA.VIEW_TABLE_USAGE b on a.TABLE_NAME = b.VIEW_NAME

left join INFORMATION_SCHEMA.VIEW_TABLE_USAGE c on b.TABLE_NAME = c.VIEW_NAME

left join INFORMATION_SCHEMA.VIEW_TABLE_USAGE d on c.TABLE_NAME = d.VIEW_NAME

left join INFORMATION_SCHEMA.VIEW_TABLE_USAGE e on d.TABLE_NAME = e.VIEW_NAME

left join INFORMATION_SCHEMA.VIEW_TABLE_USAGE f on e.TABLE_NAME = f.VIEW_NAME

 

ORDER BY a.view_name, a.table_name, b.TABLE_NAME, c.TABLE_NAME, d.TABLE_NAME, e.TABLE_NAME, f.TABLE_NAME

GO


Wednesday, June 22, 2016 - 2:02:35 AM - dhanshri Back To Top (41733)

 

 thank you so much :) 


Wednesday, December 2, 2015 - 3:28:38 PM - Yogesh Back To Top (40182)

This is exactly what I was looking for. Very nice article


Tuesday, June 5, 2012 - 12:57:36 AM - Atif Back To Top (17795)

@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.

 

 


Thursday, May 31, 2012 - 10:15:56 AM - sai Back To Top (17749)

 

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


Monday, May 14, 2012 - 12:15:51 AM - Azhar Iqbal Back To Top (17447)

Nice Information.


Monday, April 16, 2012 - 6:38:10 PM - MemphisMaven Back To Top (16952)

Thank you so much.  Very helpful!!!


Thursday, December 4, 2008 - 11:56:12 AM - jerryhung Back To Top (2331)

 Thank you

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















get free sql tips
agree to terms