Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 

Get your favorite SSC scripts directly in SSMS with the free SQL Scripts addin. Search for scripts directly from SSMS, and instantly access any saved scripts in your SSC briefcase from the favorites tab.
Download now (direct download link)

Script to find text in stored procedures

By Doug Elwood,

This is a simple script that when used by command-line OSQL/ISQL can help you locate a stored procedure.

How many times have you been looking for stored procedure code and could not find it?  This is a simple method to locate lost code.

When you manage many database servers this can become a common problem.  The scenario is familiar, you can recall working on a stored procedure, but not where it was located.

First the really easy part, create a file that contains each of your servers.  It should be a very basic text file like the following:
(dbservers.txt)
Server1
Server2
Server3

Create your script using the text in the script box and save it as a .sql file.  For this example, I used sqlfile.sql.  Please remove the comments first, they are not needed.

After saving the T-SQL script, open a command-line window (DOS Command Prompt) and type the following at the prompt:

FOR /F %s in (dbservers.txt) DO @OSQL -S%s -E -i sqlfile.sql >> search.txt

This will loop through all database servers in your file and search for the text you provide as "searchtext".

When you manage many SQL Servers this is a very handy tool to use.  Keep in mind you can use this same method to search for job steps, table names, views, functions, etc.

Total article views: 1790 | Views in the last 30 days: 2
 
Related Articles
FORUM

Scripting Stored Procedures

Scripting Stored Procedures

BLOG

SQL Script to find the last executed commands on the SQl Server

Sometimes we needs to find out the last executed commands on the database server.  Below is the SQL...

ARTICLE

Setting SQL Server Default Locations

How to set the default locations for backups, data files, and log files in SQL Server.

SCRIPT

move MDF or LDF files to new location

This stored procedure move the LDF or MDF to a new location.

FORUM

Stored Procedure (or) SQL Script Output to Text File

Stored Procedure (or) SQL Script Output to Text File

Tags
miscellaneous    
 
Contribute