71

I have a value in a table that was changed unexpectedly. The column in question is CreatedDate, which is set when my item is created, but it's being changed by a stored procedure.

Could I write some type of SELECT statement to get all the procedure names that reference this column from my table?

3
  • Have a look at the sys.all_sql_modules table. In particular the column titled definition Commented Oct 23, 2013 at 10:59
  • 1
    Possible duplicate : stackoverflow.com/questions/686247/… Commented Oct 23, 2013 at 11:00
  • I found stackoverflow.com/a/8757152/9695286 this answer more accurate Commented Apr 23, 2021 at 8:43

9 Answers 9

95

One option is to create a script file.

Right click on the database -> Tasks -> Generate Scripts

Then you can select all the stored procedures and generate the script with all the sps. So you can find the reference from there.

Or

-- Search in All Objects SELECT OBJECT_NAME(OBJECT_ID), definition FROM sys.sql_modules WHERE definition LIKE '%' + 'CreatedDate' + '%' GO -- Search in Stored Procedure Only SELECT DISTINCT OBJECT_NAME(OBJECT_ID), object_definition(OBJECT_ID) FROM sys.Procedures WHERE object_definition(OBJECT_ID) LIKE '%' + 'CreatedDate' + '%' GO 

Source SQL SERVER – Find Column Used in Stored Procedure – Search Stored Procedure for Column Name

Sign up to request clarification or add additional context in comments.

2 Comments

Does this just find objects / procedures with a defined parameter that matches the WHERE clause, or would this also find references to MyTable.SomeColumn within the objects themselves?
Doesn't work well if the name is a substring of some other names.
40

If you want to get stored procedures using specific column only, you can use try this query:

SELECT DISTINCT Name FROM sys.Procedures WHERE object_definition(OBJECT_ID) LIKE '%CreatedDate%'; 

If you want to get stored procedures using specific column of table, you can use below query :

SELECT DISTINCT Name FROM sys.procedures WHERE OBJECT_DEFINITION(OBJECT_ID) LIKE '%tbl_name%' AND OBJECT_DEFINITION(OBJECT_ID) LIKE '%CreatedDate%'; 

Comments

22

You can use ApexSQL Search, it's a free SSMS and Visual Studio add-in and it can list all objects that reference a specific table column. It can also find data stored in tables and views. You can easily filter the results to show a specific database object type that references the column

enter image description here

Disclaimer: I work for ApexSQL as a Support Engineer

1 Comment

This doesn't give just the column named FirstName in a specific SP, but in any SP. This is fine until you have the same column name in use in several tables, views,.... There is no tool I know of that can find every place a specific column is referenced.
9

You can use the system views contained in information_schema to search in tables, views and (unencrypted) stored procedures with one script. I developed such a script some time ago because I needed to search for field names everywhere in the database.

The script below first lists the tables/views containing the column name you're searching for, and then the stored procedures source code where the column is found. It displays the result in one table distinguishing "BASE TABLE", "VIEW" and "PROCEDURE", and (optionally) the source code in a second table:

DECLARE @SearchFor nvarchar(max)='%CustomerID%' -- search for this string DECLARE @SearchSP bit = 1 -- 1=search in SPs as well DECLARE @DisplaySPSource bit = 1 -- 1=display SP source code -- tables if (@SearchSP=1) begin ( select '['+c.table_Schema+'].['+c.table_Name+'].['+c.column_name+']' [schema_object], t.table_type from information_schema.columns c left join information_schema.Tables t on c.table_name=t.table_name where column_name like @SearchFor union select '['+routine_Schema+'].['+routine_Name+']' [schema_object], 'PROCEDURE' as table_type from information_schema.routines where routine_definition like @SearchFor and routine_type='procedure' ) order by table_type, schema_object end else begin select '['+c.table_Schema+'].['+c.table_Name+'].['+c.column_name+']' [schema_object], t.table_type from information_schema.columns c left join information_schema.Tables t on c.table_name=t.table_name where column_name like @SearchFor order by c.table_Name, c.column_name end -- stored procedure (source listing) if (@SearchSP=1) begin if (@DisplaySPSource=1) begin select '['+routine_Schema+'].['+routine_Name+']' [schema.sp], routine_definition from information_schema.routines where routine_definition like @SearchFor and routine_type='procedure' order by routine_name end end 

If you run the query, use the "result as text" option - then you can use "find" to locate the search text in the result set (useful for long source code).

Note that you can set @DisplaySPSource to 0 if you just want to display the SP names, and if you're just looking for tables/views, but not for SPs, you can set @SearchSP to 0.

Example result (find CustomerID in the Northwind database, results displayed via LinqPad):

Sample Result

Note that I've verfied this script with a test view dbo.TestOrders and it found the CustomerID in this view even though c.* was used in the SELECT statement (referenced table Customers contains the CustomerIDand hence the view is showing this column).


Note for LinqPad users: In C#, you can use dc.ExecuteQueryDynamic(sqlQueryStr, new object[] {... parameters ...} ).Dump(); and have the parameters as @p0 ... @pn inside the query string. Then you can write a static extension class and save it under My Extensions to be used in your LinqPad queries. The data context can be passed from the query window as DataContextBase dc via parameter, i.e. public static void SearchDialog(this DataContextBase dc, string searchString = "%") inside a public static extension class (in LinqPad 6, it is DataContext). Then you can rewrite the SQL query above as a string with parameters and invoke it from the C# context.

2 Comments

Thanks for great effort. Almost perfect. FYI - It does not return those SP which has column reference in insert into statement. I found this answer more accurate stackoverflow.com/a/8757152/9695286.
@Karan - I tried the solution you mentioned, but it returns no results. I have left a comment there.
3

i had the same problem and i found that Microsoft has a systable that shows dependencies.

SELECT referenced_id , referenced_entity_name AS table_name , referenced_minor_name as column_name , is_all_columns_found FROM sys.dm_sql_referenced_entities ('dbo.Proc1', 'OBJECT'); 

And this works with both Views and Triggers.

Comments

2

try this..

SELECT Name FROM sys.procedures WHERE OBJECT_DEFINITION(OBJECT_ID) LIKE '%CreatedDate%' GO 

or you can generate a scripts of all procedures and search from there.

Comments

2

-- Search in All Objects

SELECT OBJECT_NAME(OBJECT_ID), definition FROM sys.sql_modules WHERE definition LIKE '%' + 'ColumnName' + '%' GO 

-- Search in Stored Procedure Only

SELECT DISTINCT OBJECT_NAME(OBJECT_ID) FROM sys.Procedures WHERE object_definition(OBJECT_ID) LIKE '%' + 'ColumnName' + '%' GO 

Comments

0
SELECT * FROM sys.all_sql_modules WHERE definition LIKE '%CreatedDate%' 

Comments

0

You can use the below query to identify the values. But please keep in mind that this will not give you the results from encrypted stored procedure.

SELECT DISTINCT OBJECT_NAME(comments.id) OBJECT_NAME ,objects.type_desc FROM syscomments comments ,sys.objects objects WHERE comments.id = objects.object_id AND TEXT LIKE '%CreatedDate%' ORDER BY 1 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.