Search for text in SQL Server stored procedure is a common task for database administrators, developers, and analysts who need to locate specific logic, column references, or error messages embedded within routine code. Here's the thing — whether you are auditing security, refactoring legacy applications, or troubleshooting unexpected behavior, knowing how to efficiently query the definition of stored procedures saves time and reduces risk. This guide explains multiple T‑SQL techniques, shows practical examples, and highlights performance considerations so you can choose the approach that best fits your environment Not complicated — just consistent..
Why Search Stored Procedures?
Stored procedures encapsulate business logic, data validation, and transaction control. Over time, a database may accumulate dozens or even hundreds of routines, making manual inspection impractical. Searching for text inside these objects helps you:
- Identify dependencies – Find all procedures that reference a particular table, view, or function before renaming or dropping it.
- Detect hard‑coded values – Locate magic numbers, connection strings, or error messages that should be parameterized.
- Enforce coding standards – Verify that procedures follow naming conventions, use proper error handling, or avoid deprecated features.
- Support impact analysis – Assess the scope of changes when modifying schema objects or upgrading SQL Server versions.
- Audit security – Discover procedures that execute dynamic SQL with elevated privileges or access sensitive data.
Core System Views for Procedure Definitions
SQL Server stores the textual definition of each module (stored procedure, function, trigger, view) in several catalog views. The most reliable sources are:
| View / Function | What It Returns | Typical Use |
|---|---|---|
| sys.sql_modules | definition column containing the original SQL text |
General‑purpose text search |
| OBJECT_DEFINITION(object_id) | Scalar function returning the module definition | Inline searches within SELECT lists |
| syscomments (legacy) | text column split into 255‑character chunks |
Compatibility with very old scripts |
| sys.In real terms, dm_exec_sql_text | Retrieves the text of a batch or object from the plan cache | Finding ad‑hoc or recently executed procedures |
| **Full‑text indexes on sys. sql_modules. |
Each of these can be queried with standard T‑SQL predicates such as LIKE, CHARINDEX, or CONTAINS when a full‑text index is present.
Method 1: Querying sys.sql_modules
The sys.sql_modules view joins directly to sys.objects, allowing you to filter by object type ('P' for stored procedure) and schema.
SELECT
s.name AS SchemaName,
o.name AS ProcedureName,
m.definition
FROM sys.sql_modules AS m
INNER JOIN sys.objects AS o ON m.object_id = o.object_id
INNER JOIN sys.schemas AS s ON o.schema_id = s.schema_id
WHERE o.type = 'P' -- only stored procedures
AND m.definition LIKE '%search_term%';
Key points
- Use
%wildcards before and after the term to catch any occurrence. - For case‑insensitive searches, rely on the database collation; if you need explicit control, add
COLLATE Latin1_General_CS_AS(case‑sensitive) orCI_AS(case‑insensitive). - To avoid scanning the entire definition column for every row, consider adding a filtered index on
sys.sql_modules.definition(see the full‑text section later).
Method 2: Using OBJECT_DEFINITION in a SELECT List
When you only need the object names that match a pattern, OBJECT_DEFINITION lets you keep the query concise:
SELECT
OBJECT_SCHEMA_NAME(object_id) AS SchemaName,
OBJECT_NAME(object_id) AS ProcedureName
FROM sys.procedures
WHERE OBJECT_DEFINITION(object_id) LIKE '%search_term%';
This approach eliminates the join to sys.sql_modules but internally calls the same underlying metadata. It is handy for ad‑hoc checks in scripts or stored procedures that generate dynamic SQL.
Method 3: Leveraging syscomments (Legacy)
Older versions of SQL Server stored procedure text in the syscomments table, splitting long definitions into 255‑character rows. Although deprecated, it still exists for backward compatibility:
SELECT DISTINCT
OBJECT_NAME(id) AS ProcedureName,
OBJECT_SCHEMA_NAME(id) AS SchemaName
FROM syscomments
WHERE text LIKE '%search_term%'
AND OBJECTPROPERTY(id, 'IsProcedure') = 1;
Note: Because the definition may be split across multiple rows, you might need to aggregate the text column (e.g., FOR XML PATH('')) to reconstruct the full definition before applying a pattern that spans the split point. In most modern scenarios, prefer sys.sql_modules or OBJECT_DEFINITION.
Method 4: Finding Referencing and Referenced Objects
Sometimes you want to know which procedures reference a particular table, view, or function, or which objects a given procedure depends on. The dynamic management views sys.dm_sql_referencing_entities and `sys.
-- Procedures that reference the table dbo.Customers
SELECT
referencing