Click here to Skip to main content
15,888,239 members
Articles / Database Development / SQL Server

SQL Server – Find Column Used in Stored Procedure – Search Stored Procedure for Column Name

Rate me:
Please Sign up or sign in to vote.
4.10/5 (8 votes)
13 Aug 2012CPOL2 min read 50.9K   4   8
How to find column used in a stored procedure

Image 1

Place: Any Developer Shop

Scenario: A developer wants to drop a column from a table

Time: Any Day – usually right before developer wants to go home

The developer rushes to the manager and the following conversation begins:

Developer: I want to drop  a column from one of the tables.

Manager: Sure, just document all the places it is used in our application and come back to me.

Developer: We only use stored procedures.

Manager: Sure, then document how many stored procedures are there which are using your column and justify the modification. I will approve it once I see the documentation.

Developer back to the desk looking at hundreds of stored procedures in SSMS thinking how to find which stored procedure may be using his column. Suddenly he remembers a bookmark which he has saved earlier which had T-SQL Script to do so. He quickly opened it and ran the code.

SQL
SELECT obj.Name SPName, sc.TEXT SPText
FROM sys.syscomments sc
INNER JOIN sys.objects obj ON sc.Id = obj.OBJECT_ID
WHERE sc.TEXT LIKE '%' + 'Name Your Column Here' + '%'
AND TYPE = 'P'

The above T-SQL Script will search in the stored procedure text and return the name of the stored procedure if it will find the value specified in the WHERE condition. He was happy with his discovery and immediately created the list of the stored procedures and next action items as asked by the manager. He sent the list to the manager right after 10 minutes of his discussion with the manager. He rushed to the manager at the office to inform his promptness and realized that the manager had left for the day just few moments before.

Moral of the story: Work life balance can be maintained if we work smart!

Let us see above T-SQL Script in action. Let us assume that in AdventureWorks2012 database, we want to find the BusinessEntityID column in all the stored procedures. We can run the following T-SQL code in SSMS Query Editor and find the name of all the stored procedures.

SQL
USE AdventureWorks2012
GO
SELECT obj.Name SPName, sc.TEXT SPText
FROM sys.syscomments sc
INNER JOIN sys.objects obj ON sc.Id = obj.OBJECT_ID
WHERE sc.TEXT LIKE '%' + 'BusinessEntityID' + '%'
AND TYPE = 'P'
GO

The above T-SQL script will give results containing the name of the stored procedure and stored procedure text along with it.

Image 2

While we are discussing this subject, here are a couple of other additional related blog posts which you may find interesting.

A question to you: Is there any better way to find a column used in a stored procedure? Please leave a comment with your solution. I will post the same in this blog with due credit.

Reference: Pinal Dave (http://blog.sqlauthority.com)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Founder http://blog.SQLAuthority.com
India India
Pinal Dave is a Microsoft Technology Evangelist (Database and BI). He has written over 2200 articles on the subject on his blog at http://blog.sqlauthority.com. Along with 8+ years of hands on experience he holds a Masters of Science degree and a number of certifications, including MCTS, MCDBA and MCAD (.NET). He is co-author of two SQL Server books - SQL Server Programming, SQL Wait Stats and SQL Server Interview Questions and Answers. Prior to joining Microsoft he was awarded Microsoft MVP award for three continuous years for his contribution in community.

Comments and Discussions

 
QuestionFind all Columns referenced in a Stored Procedure Pin
Member 1571692925-Jul-22 3:01
Member 1571692925-Jul-22 3:01 
GeneralMy vote of 1 Pin
hahahahahahheheheehehehe22-Aug-13 0:39
hahahahahahheheheehehehe22-Aug-13 0:39 
QuestionSQL Server Find Pin
GeniusConnect8-Sep-12 1:18
GeniusConnect8-Sep-12 1:18 
QuestionC++? Pin
yafan16-Aug-12 8:33
yafan16-Aug-12 8:33 
What's this article got to do with C++?
AnswerRe: C++? Pin
Michael Haephrati18-Feb-13 10:11
professionalMichael Haephrati18-Feb-13 10:11 
GeneralRe: C++? Pin
yafan18-Feb-13 10:41
yafan18-Feb-13 10:41 
GeneralMy vote of 5 Pin
JammoD8713-Aug-12 23:08
JammoD8713-Aug-12 23:08 
QuestionClose, But Not Quite Pin
JonathanFahey24-Jul-12 3:49
JonathanFahey24-Jul-12 3:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.