BANCO-DE-DADOS
Searching for text inside SQL Server procedures, and why LIKE isn't that exact
In 2023 I posted the OBJECT_DEFINITION query I use at work with SQL Server databases. It works, but LIKE's _ is a wildcard, encrypted procedures come back empty and, to find who uses a table, there's a better way.
In 2023 I posted here the query I use most when I need to know which procedures contain a piece of code: a table that is about to change, a column that will be renamed, a rule someone swears is “in some procedure”. Working with old SQL Server databases, that question comes up all the time.
SELECT
name AS 'Nome da Procedure',
OBJECT_DEFINITION(object_id) AS 'Definição da Procedure'
FROM sys.procedures
WHERE OBJECT_DEFINITION(object_id) LIKE '%sua_palavra_chave%';
The post called this an “exact text” search. It isn’t quite, and that’s what this text corrects.
Everything shown here for SQL Server works since version 2005, except the dependency query, which exists since 2008.
LIKE has wildcards besides %
In SQL Server, LIKE treats %, _ (any single character) and square brackets [ ] as special. Database names
almost always have underscores. Searching for '%usp_cliente%' also finds uspXcliente and usp1cliente. For the
underscore to mean an underscore, it goes inside brackets:
WHERE OBJECT_DEFINITION(object_id) LIKE '%usp[_]cliente%'
Or with a declared escape character:
WHERE OBJECT_DEFINITION(object_id) LIKE '%usp!_cliente%' ESCAPE '!'
Upper and lower case depend on the database collation. Those with CI in the name (case insensitive), the most
common, don’t distinguish; if yours has CS, the search does.
Procedures aren’t the only place
sys.procedures only lists procedures. The same text may be in a view, a function or a trigger. sys.sql_modules
has the definition of all of them:
SELECT o.name, o.type_desc
FROM sys.sql_modules AS m
JOIN sys.objects AS o ON o.object_id = m.object_id
WHERE m.definition LIKE '%usp[_]cliente%'
ORDER BY o.type_desc, o.name;
When the definition comes back empty
OBJECT_DEFINITION returns NULL in two cases: the object was created WITH ENCRYPTION, or the connection’s user
lacks the VIEW DEFINITION permission. Both drop out of the search without warning. If a procedure you know exists
doesn’t show up, check this before doubting the text.
For “who uses this table”, there’s something better
The text search also finds comments and commented-out code, and it doesn’t find names built piece by piece in dynamic SQL. To know what depends on a table, SQL Server stores the references it finds whenever each object is created or altered:
SELECT referencing_schema_name, referencing_entity_name
FROM sys.dm_sql_referencing_entities('dbo.Clientes', 'OBJECT');
It doesn’t see dynamic SQL either, so the two queries complement each other: the dependency one for what is declared, the text one for the rest.
In other databases
In MySQL, the definition is in information_schema, and it comes back NULL for anyone without privileges on the
routine:
SELECT routine_schema, routine_name
FROM information_schema.routines
WHERE routine_definition LIKE '%sua\_palavra%';
In PostgreSQL, the source code of functions and procedures is in pg_proc, and ILIKE ignores case:
SELECT proname
FROM pg_proc
WHERE prosrc ILIKE '%sua\_palavra%';
In both, the backslash escapes the underscore.
References
- OBJECT_DEFINITION, Microsoft documentation
- LIKE and wildcard characters, Microsoft documentation
- sys.dm_sql_referencing_entities, Microsoft documentation
Who writes this
Eduardo Lira
Software developer in Rio de Janeiro. I started this blog in 2012, in technical support at a hosting company, writing down the fixes for the problems that came up every day. Today I work at Sofis Tecnologia, on critical healthcare systems and SQL Server database migrations.