Finding Specific Code in SQL Server Stored Procedures: Multiple Methods


Searching Text in Stored Procedures (SQL Server)

Using System Views:

SQL Server provides system views that offer information about database objects, including stored procedures. You can leverage these views to search for text within procedures.

Here's an example:

SELECT AS Object_Name, definition
FROM sys.sql_modules m
INNER JOIN sys.objects o ON m.object_id = o.object_id
WHERE definition LIKE '%search_text%';

This query searches for the text search_text within the definition column of the sys.sql_modules view. It joins this view with sys.objects to get the corresponding stored procedure name (Object_Name).

Using Full-Text Search (For Larger Databases):

For very large databases, system view queries might become slow. In such cases, you can consider using SQL Server's Full-Text Search feature. This involves enabling full-text search on your database and then using dedicated functions like CONTAINS to search for text within stored procedures.

Management Studio Search:

If you're using SQL Server Management Studio (SSMS), a simpler approach is to leverage its built-in search functionality. You can open the Object Explorer, navigate to your database, and then use the search bar at the top. Select "Stored Procedures" from the search dropdown and enter your search term. This will find procedures containing the specified text within their code.

Important points:

  • These methods search for literal text. Be mindful of special characters or wildcards you might need in your search string.
  • Always replace search_text with the actual text you want to find.

Using System Views (Basic Search):

SELECT schema_name(o.schema_id) AS Schema_Name, AS Object_Name, definition
FROM sys.sql_modules m
INNER JOIN sys.objects o ON m.object_id = o.object_id
WHERE definition LIKE '%discount%';

This code searches for the text "discount" (case-sensitive) within the definition of all stored procedures. It also retrieves the schema name (Schema_Name) for each procedure.

Using System Views (Search with Escape Character):

SELECT schema_name(o.schema_id) AS Schema_Name, AS Object_Name, definition
FROM sys.sql_modules m
INNER JOIN sys.objects o ON m.object_id = o.object_id
WHERE definition LIKE '%[Uu]ser%';

This code searches for the text "User" or "user" (case-insensitive) within stored procedures. The ESCAPE '\' clause specifies that the [ and ] characters within the search string should be treated literally.

Using Management Studio Search:

This method doesn't involve any code. In SQL Server Management Studio (SSMS):

  1. Open Object Explorer.
  2. Navigate to your desired database.
  3. In the search bar at the top, select "Stored procedures" from the dropdown menu.
  4. Enter your search term (e.g., "customer update").
  5. SSMS will display a list of stored procedures containing the search text within their code.


This system view provides information about database routines, including stored procedures. However, it has a limitation:


This query searches for search_text within the ROUTINE_DEFINITION column. While convenient, this view only stores the first 4000 characters of the procedure definition. So, it might not be suitable for very long stored procedures.

PowerShell Scripting:

For more automation, you can leverage PowerShell scripting. Here's a basic example (replace your_database and search_text):

$server = "your_server_name"
$database = "your_database"

Get-ChildItem -Path "SQLSERVER:\SQL\$server\$database\Procedures" | 
  ForEach-Object { 
    $procName = $_.Name
    $procDef = Get-SqlContent -ServerInstance $server -Database $database -Path ".\Procedures\$procName"
    if ($procDef -match $search_text) {
      Write-Host "Procedure: $procName contains '$search_text'"

This script iterates through stored procedures in the specified database and checks their definition for the search_text using regular expressions (-match).

Third-Party Tools:

Several third-party database management tools offer advanced search functionalities for stored procedures. These tools might provide features like:

  • Regular expression support for complex searches
  • Search across multiple databases
  • Integration with version control systems

Choosing the Right Method:

The best method depends on your specific needs. For simple searches, system views or SSMS search might suffice. For automation or complex searches, PowerShell scripting or third-party tools could be valuable options. Remember the limitations of INFORMATION_SCHEMA.ROUTINES for very long procedures.

sql sql-server stored-procedures

Parsing Names in SQL: Splitting Full Names into First, Middle, and Last Names

Understanding the Challenge:Parsing names can be tricky because of variations in name formats. While some names follow a clear "First Middle Last" structure...

Crafting Equivalent Functionality to Oracle's CREATE OR REPLACE VIEW in SQL Server

Checking for Existing View:Before modifying the view, you need to verify if it already exists. You can use the IF EXISTS statement to check for the view in the sys...

Beyond the Limits: Creative Solutions for Rearranging Columns in Your PostgreSQL Database

However, there are two main approaches to achieve a similar effect:Recreating the table:This approach involves creating a new table with the desired column order and transferring the data from the original table...

Managing User Access in SQL Server: Switching Between Single-User and Multi-User Modes

Understanding Single-User Mode:In single-user mode, only one user can access the database at a time. This mode is typically used for maintenance tasks like backups...

Ensuring Clean Data: A Guide to Unique Constraints and Indexes in PostgreSQL

Unique Constraints:Primary Function: Defines data uniqueness. You declare which columns (or combination of columns) must have distinct values across the entire table...

sql server stored procedures

Efficiently Search Stored Procedures in SQL Server 2008 and Later

Understanding Stored Procedures:Stored procedures are pre-written SQL code blocks that encapsulate a set of instructions