Alex
Alex

Reputation: 38499

Retrieving list of referenced tables from a sql view using a query

Is there a way of easily retrieving a list of tables underlying a view. For example, in the following view:

CREATE VIEW ExampleView AS
SELECT p.personId, p.surname, p.forename, p.countryCode, c.countryName
FROM persons AS p
INNER JOIN countries AS c ON p.countryCode = c.countryCode

The tables referenced are persons and countries

Is there a way of querying a system table, giving the view name, to get back the tables referenced?

I'm using SQL Server 2008

Upvotes: 0

Views: 138

Answers (1)

Aaron Bertrand
Aaron Bertrand

Reputation: 280252

SELECT * FROM sys.dm_sql_referenced_entities(N'dbo.ExampleView', N'OBJECT');

Upvotes: 1

Related Questions