Hello All,
I was trying to get all SP's referring ABS_tbl using below query. but in output it retieve all the SP's referring ABS_tbl including those having incorrect suffix.
SQL
SELECT DISTINCT so.name
FROM syscomments sc
INNER JOIN sysobjects so ON sc.id=so.id
WHERE so.type in ('P') and TEXT like '%ABC_tbl%'
--Output- 250+ SPsAlso tried below query by removing % as suffix but it returns only 1 SP and filtering even those that referred table name as ABC_tbl. Am I doing anything wrong here?
SQL
SELECT DISTINCT so.name
FROM syscomments sc
INNER JOIN sysobjects so ON sc.id=so.id
WHERE so.type in ('P') and TEXT like '%ABC_tbl'
--output 1 SP

Rajkiran SwainRank #165 · 14.4K reputationPosted Mar 31, 2023, 11:09 AM EDT
SELECT DISTINCT so.name FROM syscomments sc INNER JOIN sysobjects so ON sc.id=so.id WHERE so.type in ('P') and TEXT like '%ABC_tbl%'Naimish MakwanaRank #324 · 7.7K reputationPosted Apr 1, 2023, 10:48 AM EDT
Try below conditoin.
'%ABS_tbl%' OR '%dbo.ABS_tbl%' OR '%ABC_tbl_alias%'Thanks
Sachin SinghRank #66 · 26.2K reputationPosted Apr 1, 2023, 2:24 PM EDT
Test this
select distinct sp.Name from sys.objects o inner join sys.sql_expression_dependencies sd on o.object_id = sd.referenced_id inner join sys.objects sp on sd.referencing_id = sp.object_id and sp.type in ('P') where o.name = 'ABC_tbl' --your table name order by sp.NameAnupam MaitiRank #295 · 8.5K reputationPosted Apr 2, 2023, 1:47 AM EDT
Can you try this and let me know if it works for you.
SELECT DISTINCT o.name FROM sys.sql_modules m INNER JOIN sys.objects o ON m.object_id = o.object_id WHERE o.type = 'P' AND m.definition LIKE '%ABC_tbl%' AND m.definition NOT LIKE '%[_a-z]ABC_tbl%' ESCAPE '_'Amit MohantyRank #112 · 20K reputationPosted Apr 3, 2023, 12:07 AM EDT
SELECT DISTINCT so.name FROM syscomments sc INNER JOIN sysobjects so ON sc.id=so.id WHERE sc.text LIKE '%YourTableName%' AND so.type='P' ORDER BY so.name