Advarsel: Selvom dette er en tekstsøgningsmetode, har det script, jeg vil dele, sparet mig masser af timer . Den søger inde i:
- skalære funktioner
- funktioner med tabelværdi
- lagrede procedurer
- visninger
- triggere
Jeg var nødt til at angive en sortering for at få den til at fungere for mig.
SELECT
sys.objects.object_id,
sys.schemas.name AS [Schema],
sys.objects.name AS Object_Name,
sys.objects.type_desc AS [Type]
FROM sys.sql_modules (NOLOCK)
INNER JOIN sys.objects (NOLOCK) ON sys.sql_modules.object_id = sys.objects.object_id
INNER JOIN sys.schemas (NOLOCK) ON sys.objects.schema_id = sys.schemas.schema_id
WHERE
sys.sql_modules.definition COLLATE SQL_Latin1_General_CP1_CI_AS LIKE '%{Column Name}%' ESCAPE '\'
ORDER BY sys.objects.type_desc, sys.schemas.name, sys.objects.name
Outputtet er som følgende:
Opdater :Hvis du har brug for at søge efter en bestemt tabel, SP osv. kan du bruge en mere specialiseret forespørgsel:
DECLARE @SCHEMA_NAME VARCHAR(100) = 'dbo';
DECLARE @OBJECT_NAME VARCHAR(100) = 'MY_OBJECT';
SELECT
sys.objects.object_id,
sys.schemas.name AS [Schema],
sys.objects.name AS Object_Name,
sys.objects.type_desc AS [Type]
FROM sys.sql_modules (NOLOCK)
INNER JOIN sys.objects (NOLOCK) ON sys.sql_modules.object_id = sys.objects.object_id
INNER JOIN sys.schemas (NOLOCK) ON sys.objects.schema_id = sys.schemas.schema_id
WHERE
(
'#' + sys.sql_modules.definition + '#' COLLATE SQL_Latin1_General_CP1_CI_AS LIKE '%[^a-z_]'[email protected]_NAME+'.'[email protected]_NAME+'[^a-z_]%' ESCAPE '\'
OR '#' + sys.sql_modules.definition + '#' COLLATE SQL_Latin1_General_CP1_CI_AS LIKE '%[^a-z_]\['[email protected]_NAME+'\].'[email protected]_NAME+'[^a-z_]%' ESCAPE '\'
OR '#' + sys.sql_modules.definition + '#' COLLATE SQL_Latin1_General_CP1_CI_AS LIKE '%[^a-z_]'[email protected]_NAME+'.\['[email protected]_NAME+'\][^a-z_]%' ESCAPE '\'
OR '#' + sys.sql_modules.definition + '#' COLLATE SQL_Latin1_General_CP1_CI_AS LIKE '%[^a-z_]\['[email protected]_NAME+'\].\['[email protected]_NAME+'\][^a-z_]%' ESCAPE '\'
)
ORDER BY sys.objects.type_desc, sys.schemas.name, sys.objects.name
P.S. :Begge forespørgsler søger også i kommentarer.