Home > SQL Server Tips > Database Development > Find the right stored procedure
SQL Server Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

DATABASE DEVELOPMENT

Find the right stored procedure


CADE BRYANT
04.13.2001
Rating: --- (out of 5)


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


Let's say you work with a huge MS SQL Server database containing hundreds of user-created stored procedures. You need to find a stored procedure that performs a certain function necessary for your application, but you don't want to spend hours running sp_helptext and examining the code for 300 stored procedures. What can you do?

Here's a stored procedure I created called sp_FindProcTxt that allows you to search all the user stored procedures in your database, returning the ones whose code contains the search string you specify. There are 2 modes: 0 (the default) returns only the names of the SPs; Mode 1 returns the entire code of the SPs.

For example, if you need to find a SP that re-indexes all the tables in your database, and thus you want to list all SPs (along with their code) that contain the word "index," you would type:

EXEC sp_FindProcTxt 'index', 1

Below is the code (note: the sysname datatype works only with SQL 2000; for SQL 7 you have to use nvarchar(128)):

create procedure sp_findproctxt
/*
lists all stored prodedures that contain the 
text in @sstring in their code
*/
@sstring sysname,
@mode int = 0
/*
0 = return sp name only
1 = return entire code of sp
*/

as

if @mode = 0 --we just want the names
begin

select o.name
from sysobjects o
join syscomments c
on o.id = c.id
where o.type = 'p'
and charindex(@sstring, c.text) >= 1

end

if @mode = 1 --we need the entire code
begin

declare findproctxt cursor
for
select o.name
from sysobjects o
join syscomments c
on o.id = c.id
where o.type = 'p'
and charindex(@sstring, c.text) >= 1

declare @proc sysname

open findproctxt
fetch next from findproctxt
into @proc

while (@@fetch_status = 0)
begin  exec sp_helptext @proc
 fetch next from findproctxt
 into @proc
end

close findproctxt
deallocate findproctxt

end

For More Information


Rate this Tip
To rate tips, you must be a member of SearchSQLServer.com.
Register now to start rating these tips. Log in if you are already a member.




Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   



RELATED CONTENT
SQL Server Stored Procedures
SQL Server Mailbag: CALs, witnesses and unwanted changes
SQL Server Mailbag: Stored procedures, triggers and SSRS reports
Top tips and tricks for SQL Server database development
Top 10 SQL Server development tips of 2008
SQL Server trigger vs. stored procedure to receive data notification
SQL Server errors, failures and other problems fixed from the trenches
SQL Server and data manipulation in T-SQL
How to use SQL Server 2008 hierarchyid data type
SQL Server stored procedures tutorial: Write, tune and get examples
Check SQL Server database and log file size with this stored procedure

Database Development
Using DELETE and TRUNCATE TABLE statements to delete data in SQL Server
Speed up reports in SQL Server Reporting Services with caching
Data Transformation Services vs. SSIS: The key differences
Working with IntelliSense in SQL Server 2008 Management Studio
Top tips and tricks for SQL Server database development
Managing the development lifecycle with Visual Studio Team System 2008
Processing XML files with SQL Server functions
A first look at Visual Studio Team System 2008 Database Edition
How to create a SQL inner join and outer join: Basics to get started
New datetime data types in SQL Server 2008 offer flexibility

RELATED GLOSSARY TERMS
Terms from Whatis.com − the technology online dictionary
library  (SearchSQLServer.com)
trigger  (SearchSQLServer.com)

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary

DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.



SQL Server Development - .NET, C#, T-SQL, Visual Basic
HomeNewsTopicsITKnowledge ExchangeTipsAsk the ExpertsMultimediaWhite PapersIT Downloads
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 2005 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts