简体   繁体   中英

identify insert stored proc

How do I identify stored proc that inserts records into "TableXYZ"?

I could have hundreds of stored procs.

Please help.

Use:

SELECT OBJECT_NAME(m.object_id), m.*
  FROM SYS.SQL_MODULES m
 WHERE m.definition like N'%INSERT INTO my_table_name%'

This will allow you to search for table reference(s).

Background:

SYSCOMMENTS and INFORMATION_SCHEMA.routines have NVARCHAR(4000) columns, while SYS.SQL_MODULES definition column is NVARCHAR(MAX). So if "my_table_name" is used at position 3998, it won't be found. SYSCOMMENTS does have multiple lines, but ROUTINES truncates.

I believe that you should be able to view the dependencies of the table by right clicking it on the management studio. This should at the very lease show you all stored procedures working with that table.

SELECT * FROM sys.sql_modules
WHERE definition like '%insert%TableXYZ%'

...gets you on the road to an answer.

SELECT ROUTINE_NAME, ROUTINE_DEFINITION 
FROM INFORMATION_SCHEMA.ROUTINES 
WHERE ROUTINE_DEFINITION LIKE '%insert into dbo.tablexyz%' 
AND ROUTINE_TYPE='PROCEDURE'

You will need to adjust the like clause.

The add-in RedGate SQL Search , currently free, can be very helpful as it lets you search all of the code in your database. You could search for just the table name and see if that gets you close enough. If you consistently wrote your INSERT statement to look like INSERT INTO TableName , you could search for that entire phrase.

To do this kind of search manually as an T-SQL query, you can query the definition column in the sys.sql_modules system view. This provides you a lot of flexibility as you can use LIKE and/or PATINDEX to search for wildcards/patterns.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM