Find Stored Procedure Related to Table in SQL Server

Hello to everyone,

In this article, I will talk about how to find the tables used by Stored procedures in SQL Server.

You can easily do this using the code below.

DECLARE @temptableforSP TABLE
(
    spName VARCHAR(100),
    tableName NVARCHAR(100)
);
DECLARE @SP_Name AS NVARCHAR(100);
DECLARE @SP_Cursor AS CURSOR;
SET @SP_Cursor = CURSOR FOR
SELECT [name]
FROM sys.objects
WHERE type = 'P';
OPEN @SP_Cursor;
FETCH NEXT FROM @SP_Cursor
INTO @SP_Name;
WHILE @@FETCH_STATUS = 0
BEGIN
    INSERT INTO @temptableforSP
    SELECT OBJECT_NAME(referencing_id) AS referencing_entity_name,
           referenced_entity_name
    FROM sys.sql_expression_dependencies AS sed
        INNER JOIN sys.objects AS o
            ON sed.referencing_id = o.object_id
    WHERE referencing_id = OBJECT_ID(@SP_Name);
    FETCH NEXT FROM @SP_Cursor
    INTO @SP_Name;
END;
CLOSE @SP_Cursor;
DEALLOCATE @SP_Cursor;
SELECT *
FROM @temptableforSP;

When you run the code on the relevant database, you will get a result as follows.

Find Stored Procedure Related to Table in SQL Server

As you can see, we have obtained the tables as we wanted.

Good luck to everyone in business and life.

543 Views

Yavuz Selim Kart

I try to explain what I know in software and database. I am still improving myself by doing research on many programming languages. Apart from these, I am also interested in Graphic Design and Wordpress. I also have knowledge about SEO and Social media management. In short, I am a determined person who likes to work hard.

You may also like...

Don`t copy text!