T-SQL: Show stored procedures related to tables, c

2020-07-23 04:56发布

问题:

I'm using the following t-sql code:

USE [my_database]
SELECT DISTINCT so.name
FROM syscomments sc
INNER JOIN sysobjects so ON sc.id=so.id
WHERE sc.TEXT LIKE '%table_name%'

in order to show all the Stored Procedures that use the table table_name.

I want do this work for all tables in my database.

How can I perform this task and organize the output?

回答1:

This uses information schema for both tables, and stored procedures. You can change or get rid of ROUTINE_TYPE condition to add functions, and you can change table type to return views.

This answer produces its results by checking what tables a stored procedure depends on. I think this will be a much more accurate result then checking if a name is in the query text. If the procedure refers to a table in a comment section, then this result will not be returned in the first query, but will be in the second and other answers given.

SELECT t.TABLE_NAME, s.ROUTINE_NAME
FROM INFORMATION_SCHEMA.TABLES t
INNER JOIN INFORMATION_SCHEMA.ROUTINES s ON
    s.ROUTINE_NAME IN (SELECT referencing_entity_name 
        FROM sys.dm_sql_referencing_entities(TABLE_SCHEMA + '.' + TABLE_NAME, 'OBJECT'))
    AND s.ROUTINE_TYPE = 'PROCEDURE'
WHERE t.TABLE_TYPE = 'BASE TABLE'

edit: Here's how to get the dependencies without the function. (I like this method the best)

SELECT DISTINCT t.name [TableName], p.name [ProcedureName]
FROM sys.objects t 
LEFT JOIN sys.sql_dependencies d ON
    d.referenced_major_id = t.object_id
LEFT JOIN sys.objects p ON
    p.object_id = d.object_id
    AND p.type = 'p'
WHERE t.type = 'u'

If your specific use is to just find any string that matches a table name, below will work:

SELECT t.TABLE_NAME, s.ROUTINE_NAME 
FROM INFORMATION_SCHEMA.TABLES t
INNER JOIN INFORMATION_SCHEMA.ROUTINES s 
    ON CHARINDEX(t.TABLE_NAME, s.ROUTINE_DEFINITION) > 0
    AND s.ROUTINE_TYPE = 'PROCEDURE'
WHERE t.TABLE_TYPE = 'BASE TABLE'


回答2:

You could do a JOIN on LIKE:

select * from INFORMATION_SCHEMA.TABLES t
join
(
    SELECT DISTINCT so.name
    FROM syscomments sc
    INNER JOIN sysobjects so ON sc.id=so.id
) x on x.name like '%' + t.TABLE_NAME + '%'

Note that your query doesn't restrict to procs - you'll also get views, defaults, and other objects too. If you just want procs, you can add where so.xtype = 'P' to your inner query.



回答3:

Another version that uses sys tables only:

select t.name as TableName, p.name as SPName
from sys.objects t
join sys.syscomments c
    on c.text like '%' + t.name + '%'
join sys.objects p
    on p.object_id = c.id
where t.type = 'U' -- user table
and p.type = 'P'   -- procedure 


回答4:

You can also use the built in function that's been around at least since SQL 2005 and works for tables, views, and stored procedures. I get the same number of results as Daniel's answer above when checking dependencies on a table in a fairly enterprisy database.

sp_depends [TableName]

sp_depends [TableName.Column]

sp_depends [StoredProcedureName]

http://msdn.microsoft.com/en-us/library/ms189487(v=sql.90).aspx