I have a database where i have list of tables,procedures,views and triggers. But i want a query to get all the dependencies of a table including child tables which are referring the parent table.
SqlTableDependency is a high-level implementation to access table record change notifications from SQL Server. This class allows you to detect changes on a database table.
Using SQL Server Management Studio Expand Databases, expand the database in which the procedure belongs, and then expand Programmability. Expand Stored Procedures, right-click the procedure and then click View Dependencies. View the list of objects that depend on the procedure.
You can actually see the queries that SQL Developer runs under the hood. If you go to to the View menu and choose Log, or hit Ctrl Shift L (assuming you're using Windows) you'll get a docked window which by default is titled "Messages - Log". At the bottom of that are two tabs, with "Messages" selected.
The following are the ways we can use to check the dependencies:
Method 1: Using sp_depends
sp_depends 'dbo.First' GO
Method 2: Using information_schema.routines
SELECT * FROM information_schema.routines ISR WHERE CHARINDEX('dbo.First', ISR.ROUTINE_DEFINITION) > 0 GO
Method 3: Using DMV sys.dm_sql_referencing_entities
SELECT referencing_schema_name, referencing_entity_name, referencing_id, referencing_class_desc, is_caller_dependent FROM sys.dm_sql_referencing_entities ('dbo.First', 'OBJECT'); GO
In SQL Server 2008 there are two new Dynamic Management Functions introduced to keep track of object dependencies: sys.dm_sql_referenced_entities and sys.dm_sql_referencing_entities:
1/ Returning the entities that refer to a given entity:
SELECT referencing_schema_name, referencing_entity_name, referencing_class_desc, is_caller_dependent FROM sys.dm_sql_referencing_entities ('<TableName>', 'OBJECT')
2/ Returning entities that are referenced by an object:
SELECT referenced_schema_name, referenced_entity_name, referenced_minor_name, referenced_class_desc, is_caller_dependent, is_ambiguous FROM sys.dm_sql_referenced_entities ('<StoredProcedureName>', 'OBJECT');
Alternatively, you can use sp_depends:
EXEC sp_depends '<TableName>'
Another option is to use a pretty useful tool called SQL Dependency Tracker from Red Gate.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With