Click here to Skip to main content
15,880,796 members
Articles / Database Development / SQL Server
Alternative
Tip/Trick

How to Find a Text in SQL Server Stored Procedure

Rate me:
Please Sign up or sign in to vote.
2.40/5 (4 votes)
29 Jun 2012CPOL 52.4K   1   3
This is an alternative for "Find all Stored Procedures having a given text in it"

Introduction

This article describes How to find a Text in Stored Procedure (Checked on SQL Server 2008)

Background

Need to delete or rename a Column Name in Table? 100's of Stored Procedures? Running short on time? Constraints may save you by generating an warning but Stored Procedure Won't!!!

Here is an awesome Trick that may save your time!!!

This applies to non encrypted stored procedues only.

  • Open SQL Server Management Studio
  • Make sure that you have permission to access the database objects
  • Select the Database which has Stored Procedure in which you need to find Text
  • Click on New Query
  • Below query searches for Text Product and Order [You can use AND or OR and more]

Using the code

SQL
SELECT Name as [Stored Procedure Name]
FROM sys.procedures
WHERE OBJECT_DEFINITION(OBJECT_ID) LIKE '%Product%'
AND OBJECT_DEFINITION(OBJECT_ID) LIKE '%Code%'

Screen Shot

I am working on Find and Replace version of above example once done will post here. Enjoy

This article was posted on my blog http://www.smartcodar.com/content.php?124-How-to-Find-a-Text-in-SQL-Server-Stored-Procedure added here with some enhancement

License

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


Written By
India India
I am a Tech Lead / .Net Architect,

I love working with Business Challenges, Requirements

I work mainly for Designing eCommerce Portals Using ASP.Net enabled Frameworks such as Mediachase, zNode and Storefront.

I love writing my Articles at Code Project and my site www.SmartCodar.com.

Comments and Discussions

 
Answerhere is a way for doing it against all databases in the corresponding instance Pin
Jose Pla23-Jun-14 11:25
Jose Pla23-Jun-14 11:25 
I had to do the search for a linked server that was going to be decommissioned and was beign referred by different DBs and its SPs

SQL
IF OBJECT_ID('tempdb..##alldbs','U') IS NOT NULL
begin
  truncate table ##alldbs
  drop table ##alldbs
end

create table ##alldbs (
Server nvarchar(100),
DB nvarchar(100),
type nvarchar(50),
object_name nvarchar(200),
text nvarchar(max)
)

EXEC sp_msforeachdb N'
use [?]
insert into ##alldbs
select @@servername,DB_NAME()DATABASE_NAME, SSO.type, SSO.name, SM.definition
from sys.sysobjects SSO
join sys.sql_modules SM on SSO.id = SM.object_id
WHERE SM.definition LIKE ''%<YOUR SEARCH STRING>%'''

select * from ##alldbs
order by DB,object_name,type


modified 17-Jul-14 18:36pm.

GeneralMy vote of 1 Pin
Robert Dondo26-Mar-13 3:21
Robert Dondo26-Mar-13 3:21 
GeneralMy vote of 1 Pin
Selvin1-Jul-12 23:06
Selvin1-Jul-12 23:06 

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.