Finding a literal within a database definition - sql-server

A database contains in its definition some hardcoded literal, which should be replaced by a generic function call.
The literal may appear in procedures, triggers, defaults, checks, etc. - basically "everywhere".
I'm using this code to find the literal:
SELECT Name
FROM sys.objects
WHERE OBJECT_DEFINITION(OBJECT_ID) LIKE '%HARDCODEDLITERAL%'
I just would like to know if this code is sufficient to find all occurrences, or if I might be overseeing some occurrences within the database ?

You can try like this:
SELECT DISTINCT o.name AS Object_Name,o.type_desc
FROM sys.sql_modules m
INNER JOIN sys.objects o ON m.object_id=o.object_id
WHERE m.definition Like '%HARDCODEDLITERAL%'

Related

Find a specific text string in a Stored Procedure, Function, View or Trigger

I want to find a text or column name where I use in Stored Procedure, Function, View and Trigger.
You can easily query the stored object definitions in numerous ways, one way is
select o.[name],o.type_desc
from sys.sql_modules m
join sys.objects o on o.object_id=m.object_id
where m.definition like '%<search text>%'
order o.type_desc, o.[name]

Find all stored procedures including a text in their body

I saw some answers about search text in stored procedures name, Id and etc. But I don't find out that what is the good way to find all stored procedures including the SQL of their body
More info: I have a table called X; I wand to drop it and I want to be sure that it has not been used in stored procedures.
this will find all procedures, functions, triggers, etc with the text Xin it
SELECT DISTINCT
o.name AS Object_Name,
o.type_desc,
m.*
FROM sys.sql_modules m
INNER JOIN sys.objects o ON m.object_id = o.object_id
WHERE m.definition Like '%X%'
It is easy to alter so you only get procedures, or whatever you need

How to know which stored procedure has cross database query

I want to know which stored procedure has cross database/ linked server call.
I know i can do some kind string search with database name being string to search.
One of the example of it as below
SELECT DISTINCT
o.name AS Object_Name,
o.type_desc
FROM sys.sql_modules m
INNER JOIN
sys.objects o
ON m.object_id = o.object_id
WHERE m.definition Like '%{{DBNAME}}%';
but if possible then i do not want to go for string search so is there any other way to find all procedure which might have cross database/ linked server ?
There is a system view sys.sql_expression_dependencies that provides exactly this information:
select d.*
from sys.sql_expression_dependencies d
where d.referenced_database_name is not null
and d.is_ambiguous = 0;
Pay attention, however, that some of the references might be "fake", or rather not what you expect. For example, XML methods are also listed in it, which might cause some confusion.

How To Find all Stored Procedure that are using a specific function

Hello is there a way that I can easily find all stored procedure in SQL Server that are using a specific function like for ex. function fn_Test()?
I want to see all the stored procedures using this function.
Thanks
This should do what you are after, it will return any sproc with the entered text within it somewhere:
SELECT DISTINCT so.name
FROM syscomments sc
INNER JOIN sysobjects so ON sc.id=so.id
WHERE sc.TEXT LIKE '%fn_Test%'
You can use,
SELECT obj.Name SPName, sc.TEXT SPText
FROM sys.syscomments sc
INNER JOIN sys.objects obj ON sc.Id = obj.OBJECT_ID
WHERE sc.TEXT LIKE '%' + 'YOUR_FUNCTION_NAME' + '%'
AND TYPE = 'P'
This snippet of sql will list all the stored procedures that contain the name of the function. However that could include content in comments. I think this would be adequate for the job though
select distinct name from sysobjects
where type = 'P'
and id in (select id from syscomments where text like '%fn_Test%')
order by name
Take a look at sys.dm_sql_referencing_entities.

Is it possible to find the objects which depend on a synonym?

I tried exec sp_depends #objname = 'sfel.elpc' but I did not get any results, but I know the synonym is referenced in at least one stored procedure.
try:
SELECT DISTINCT
o.name,o.type_desc
FROM sys.sql_modules m
INNER JOIN sys.objects o ON m.object_id=o.object_id
WHERE m.definition Like '%sfel.elpc%'
You could try SQL Search by Red Gate, which is free. I'm by no means sure whether it supports synonyms, but might be worth a try if no other solutions are suggested.
this code is better:
SELECT
*
FROM sys.sql_modules m
INNER JOIN sys.objects o ON m.object_id=o.object_id
WHERE m.definition Like '%sfel.elpc%' and type = 'p'
this code give you a more limited list of objects.

Resources