178

I created a table on development environment for testing purpose and there are few sp's which are refreing this table. Now I have have to drop this table as well as identify all sp's which are referring this table. I am facing difficulty to find list of all sp's. Please suggest some query by assuming that the table name is 'x' and database is sql server 2005.

Cœur
  • 37,241
  • 25
  • 195
  • 267
DJay
  • 2,457
  • 2
  • 18
  • 22
  • 1
    possible duplicate of [SQL Server 2005 - Find Which Stored Procs Run To A Particular Table](http://stackoverflow.com/questions/3361141/sql-server-2005-find-which-stored-procs-run-to-a-particular-table) – Martin Smith Sep 01 '11 at 14:58
  • 1
    possible duplicate of [how to search Sql Server 2008 R2 stored procedures for a string?](http://stackoverflow.com/questions/6354256/how-to-search-sql-server-2008-r2-stored-procedures-for-a-string) or http://stackoverflow.com/questions/3991030/searching-in-sql-management-studio-2005/3991077#3991077 – gbn Sep 01 '11 at 15:02
  • 1
    to the experts : how about for a non-MS SQL server? – Deian Sep 04 '15 at 19:38

13 Answers13

355
SELECT Name
FROM sys.procedures
WHERE OBJECT_DEFINITION(OBJECT_ID) LIKE '%TableNameOrWhatever%'

BTW -- here is a handy resource for this type of question: Querying the SQL Server System Catalog FAQ

Smandoli
  • 6,919
  • 3
  • 49
  • 83
Chains
  • 12,541
  • 8
  • 45
  • 62
  • 3
    Is there any reason to do this over accessing the `ROUTINE_DEFINITION` on `INFORMATION_SCHEMA.ROUTINES`? – Marie Jun 23 '16 at 16:54
  • 1
    @Marie -- SQL-Server 2005 (AFAIK) doesn't have that. – Chains Aug 10 '16 at 22:57
  • 2
    Question, does this solution find referenced objects when they are referenced *inside* of a string? like, `set @Query = “SELECT * FROM Object_I_Need_To_Find_References…”; ` – Jeff.Clark Nov 07 '16 at 17:16
  • 1
    @Jeff.Clark -- There's an easy way to find out :-) -- but yes, it searches the text of the procedure directly, so it would find that. – Chains Dec 28 '16 at 21:55
  • 2
    yeah yeah :) It was going to take me a while to set up the test, and I was just looking for a lazy/easy answer :) – Jeff.Clark Dec 31 '16 at 01:09
  • @Chains Is there a way to limit to only if the table is used for insert, update, delete? – Hassan Voyeau Aug 21 '18 at 07:39
  • Not a revolutionary change, but sorting by name: SELECT Name FROM sys.procedures WHERE OBJECT_DEFINITION(OBJECT_ID) LIKE '%FrameCardPortTiePairCable%' ORDER BY Name – DubDub Dec 18 '19 at 08:35
  • For some reason, this is not working for me. It's returning 0 rows. – jukebox Sep 11 '20 at 06:50
  • 1
    @Marie I think ROUTINE_DEFINITION column in INFORMATION_SCHEMA.ROUTINES may not have full definition if it exceeds 4k chars -- https://stackoverflow.com/a/31334753/ – Xu Shaoyang Dec 17 '21 at 11:49
  • This helps! Just that if a table name got mentioned in comments of a procedure, this method might not be always accurate. – Xu Shaoyang Dec 17 '21 at 11:52
  • `Name` should be `name` , and also `OBJECT_ID` should be `object_id` – Sherif Riad Feb 10 '23 at 11:32
37

The following works on SQL2008 and above. Provides a list of both stored procedures and functions.

select distinct [Table Name] = o.Name, [Found In] = sp.Name, sp.type_desc
  from sys.objects o inner join sys.sql_expression_dependencies  sd on o.object_id = sd.referenced_id
                inner join sys.objects sp on sd.referencing_id = sp.object_id
                    and sp.type in ('P', 'FN')
  where o.name = 'YourTableName'
  order by sp.Name
Guy Hollington
  • 566
  • 5
  • 4
32

sometimes above queries will not give correct result, there is built in stored procedure available to get the table dependencies as:

EXEC sp_depends @objname = N'TableName';
Raj Baral
  • 661
  • 6
  • 19
Pramod Pawar
  • 321
  • 3
  • 2
21

A non-query way would be to use the Sql Server Management Studio.

Locate the table, right click and choose "View dependencies".

EDIT

But, as the commenters said, it is not very reliable.

Hans Kesting
  • 38,117
  • 9
  • 79
  • 111
  • 4
    In 2005 the dependencies information is unreliable if objects are not created in the correct order. – Martin Smith Sep 01 '11 at 15:30
  • 3
    As @Martin Smith noted, a stored procedure referencing a non-existent object will get created, albeit with a warning, but not entry will be placed in sysdepends. Also, the same holds true for stored procedures referencing a table in a foreign database: No entry is ever placed in sysdepends in either database. Antoher feature is that dropping/recreating a table or view breaks the dependency chain. These...err...features make SQL Server's dependency tracking pretty much useless. – Nicholas Carey Sep 01 '11 at 16:31
8

The following query will fetch all Stored Procedure names and the corresponding definition of those SP's

select 
   so.name, 
   text 
from 
   sysobjects so, 
   syscomments sc 
where 
   so.id = sc.id 
   and UPPER(text) like '%<TABLE NAME>%'
Chains
  • 12,541
  • 8
  • 45
  • 62
Deepak Kothari
  • 1,601
  • 24
  • 31
8

In management studio you can just right click to table and click to 'View Dependencies' enter image description here

than you can see a list of Objects that have dependencies with your table :enter image description here

nzrytmn
  • 6,193
  • 1
  • 41
  • 38
6
SELECT
    o.name
FROM
    sys.sql_modules sm
INNER JOIN sys.objects o ON
    o.object_id = sm.object_id
WHERE
    sm.definition LIKE '%<table name>%'

Just keep in mind that this will also turn up SPs where the table name is in the comments or where the table name is a substring of another table name that is being used. For example, if you have tables named "test" and "test_2" and you try to search for SPs with "test" then you'll get results for both.

Tom H
  • 46,766
  • 14
  • 87
  • 128
  • 2
    Querying `syscomments` in this manner is not reliable for long procedures as it splits the definition into 4000 character chunks across multiple rows. `sys.sql_modules` avoids this. – Martin Smith Sep 01 '11 at 14:59
  • 1
    Good point. I've updated it to use `sys.sql_modules` instead. – Tom H Sep 01 '11 at 15:03
  • 3
    syscomments has been debunked in every one of my dozen or so answers to the same question http://stackoverflow.com/search?tab=votes&q=user%3a27535%20syscomments – gbn Sep 01 '11 at 15:03
5

The query below works only when searching for dependencies on a table and not those on a column:

EXEC sp_depends @objname = N'TableName';

However, the following query is the best option if you want to search for all sorts of dependencies, it does not miss any thing. It actually gives more information than required.

 select distinct
        so.name
        --, text 
  from 
       sysobjects so, 
       syscomments sc 
  where 
     so.id = sc.id 
     and lower(text) like '%organizationtypeid%'
  order by so.name
Siraj Ansari
  • 51
  • 1
  • 1
3
SELECT DISTINCT OBJECT_NAME(OBJECT_ID),
object_definition(OBJECT_ID)
FROM sys.Procedures
WHERE object_definition(OBJECT_ID) LIKE '%' + 'table_name' + '%'

GO

This will work if you have to mention the table name.

Neels
  • 2,547
  • 6
  • 33
  • 40
ric
  • 31
  • 1
1

You have basically 2 options:

----Option 1

SELECT DISTINCT so.name
FROM syscomments sc
INNER JOIN sysobjects so ON sc.id=so.id
WHERE sc.TEXT LIKE '%tablename%'

----Option 2

SELECT DISTINCT o.name, o.xtype
FROM syscomments c
INNER JOIN sysobjects o ON c.id=o.id
WHERE c.TEXT LIKE '%tablename%'

These 2 queries will get you all the stored procedures that are referring the table you desire. This query relies on 2 sys tables which are sysobjects and syscomments. The sysobjects is where all of your DB object names are stored this includes the stored procedures.

The syscomments contains the text for all of your procedures.

If you query: SELECT * FROM syscomments

You'll have a table containing the id which is the mapping to the sysobjects table with the text contained in the stored procedures as the last column.

1

This useful query also works if you are using Azure SQL / Synapse Anlaytics

SELECT DISTINCT o.name AS Object_Name,o.type_desc
FROM sys.sql_modules m
INNER JOIN sys.objects o
ON m.object_id=o.object_id
WHERE m.definition Like '%Table_Name%'
Cleptus
  • 3,446
  • 4
  • 28
  • 34
0

Try This

   SELECT DISTINCT so.name
    FROM syscomments sc
    INNER JOIN sysobjects so ON sc.id=so.id
    WHERE sc.TEXT LIKE '%your table name%'
Udara Kasun
  • 2,182
  • 18
  • 25
-1
SELECT DISTINCT o.name, o.xtype
FROM syscomments c
INNER JOIN sysobjects o ON c.id=o.id
WHERE c.TEXT LIKE '%tablename%'
Jin Lee
  • 3,194
  • 12
  • 46
  • 86
Ajay
  • 75
  • 5