First of all we shouldn’t use the system stored procedure sp_depends for this as it is not reliable. For more details on sp_depends you can refer to the article sp_depends results are not reliable.
For this in Sql server 2008 new Dynamic Management Function sys.dm_sql_referenced_entities is introduced. This dynamic management function provides all the entities in the current database which are referenced by a stored procedure or function.
Let us understand this with an example. First create a table, then a stored procedure which references the table.
CREATE DATABASE DemoSQLHints GO USE DemoSQLHints GO CREATE TABLE dbo.Employee ( Id int IDENTITY(1,1), FirstName NVarchar(50), LastName NVarchar(50) ) GO INSERT INTO dbo.Employee(FirstName, LastName) VALUES('BASAVARAJ','BIRADAR') GO CREATE PROCEDURE dbo.GetEmployeeDetails AS BEGIN SELECT * FROM dbo.Employee END GO
Now we can use a script like below to find all the entities in the current database which are referenced by the stored procedure dbo.GetEmployeeDetails
SELECT referenced_schema_name, referenced_entity_name, referenced_minor_name FROM sys.dm_sql_referenced_entities('dbo.GetEmployeeDetails', 'OBJECT') GO
Result:
Note: 1) This Dynamic Management Function is introduced as a part of Sql Server 2008. So above script works in Sql Server version 2008 and above.
2) While specifying the stored procedure name please include schema name also, otherwise referenced objects list will not be displayed.
You may also like to read my other articles:
How to find all dependencies of a table in Sql Server?
How to Insert Stored Procedure result into a table in Sql Server?
Please correct me if my understanding is not correct. Comments are always welcome.