View the Dependencies of a Table in MSDN shows alternative methods of getting this information: SSMS and T-SQL code. However, none of sys.sql_expression_dependencies, sys.dm_sql_referencing_entities and sys.dm_sql_referenced_entities works for me.
Consider the following code:
CREATE TABLE dbo.master(
c1 INT not null
CONSTRAINT PK_master_c1 PRIMARY KEY(c1)
);
GO
CREATE TABLE dbo.detail(
c1 INT
CONSTRAINT FK_master_c1_detail_c1 FOREIGN KEY(c1) references dbo.master(c1)
);
GO
In SSMS, if I right click the dbo.master table and select "View Dependencies", it shows that dbo.detail depends on dbo.master and dbo.master depends on nothing. However, when I run the query:
SELECT * FROM sys.sql_expression_dependencies WHERE referencing_id = OBJECT_ID(N'dbo.master');
to view the objects that depend on dbo.master, the result set is empty. The following queries also return nothing:
SELECT * FROM sys.dm_sql_referencing_entities('dbo.master', 'OBJECT');
SELECT * FROM sys.dm_sql_referenced_entities('dbo.master', 'OBJECT');
SELECT * FROM sys.dm_sql_referencing_entities('dbo.detail', 'OBJECT');
SELECT * FROM sys.dm_sql_referenced_entities('dbo.detail', 'OBJECT');
Questions:
1) Does the MSDN article explain the queries correctly? It says that to show the objects that depend on Production.vProductAndDescription, you should write "WHERE referencing_id = OBJECT_ID(N'Production.vProductAndDescription');" and to show the
objects on which
Production.vProductAndDescription depends, you should write "WHERE referenced_id = OBJECT_ID(N'Production.Product');". Surely it should be the other way round?
2) Is it wrong that these queries are returning nothing? In my example, dbo.detail is not a view based on dbo.master. Neither is it a stored procedure that refers to dbo.master. It is a table with a foreign key that references dbo.master. Still, MSDN suggests
sys.sql_expression_dependencies as an alternative to SSMS, so I was expecting it to show the same information.
I am running SQL Server 2012 Express 64-bit on Windows 8. My login (Windows Authentication) is a member of the sysadmin server role and is the creator of the database in which I am working. This database has just the two tables in it.
Microsoft SQL Server 2012 (SP1) - 11.0.3128.0 (X64)
Dec 28 2012 20:23:12
Copyright (c) Microsoft Corporation
Express Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: )