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

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.

Related

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.

Finding a literal within a database definition

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%'

list of views referencing a table

Is there any way to know if a particular table is being referenced by any Views or not.I used the below code which gives only SP's and function names:
select * from sys.objects p inner join sys.sql_modules m
on p.object_id = m.object_id
where m.definition like '%abc%'
Please help!!!
select *
from INFORMATION_SCHEMA.VIEWS
where VIEW_DEFINITION like '%abc%'
First, your query gives views in the result set (I tried it on AdentureWorks2012 -> Production.Product table):
If you're using SQL Server 2008 or above, you can use the sys.sql_expression_dependencies catalog view. For example:
SELECT
referencing_object_name = o.name,
referencing_object_type_desc = o.type_desc,
referenced_object_name = referenced_entity_name,
referenced_object_type_desc = o1.type_desc
FROM sys.sql_expression_dependencies sed
INNER JOIN sys.objects o
ON sed.referencing_id = o.[object_id]
LEFT OUTER JOIN sys.objects o1
ON sed.referenced_id = o1.[object_id]
WHERE referenced_entity_name = 'YourTable'
It will give you nice look on each by-name dependency on a user-defined entity
For column level dependencies you can use the sys.dm_sql_referenced_entities function
Hope this helps
If you need to find database objects (e.g. tables, columns, triggers) by name - have a look at the FREE Red-Gate tool called SQL Search which does this - it searches your entire database for any kind of string(s).
It's a great must-have tool for any DBA or database developer - did I already mention it's absolutely FREE to use for any kind of use??
If that doesn't fit your bill - you could also check out the sysdepends catalog view in SQL Server - it lists what objects depend on what (see details in the MSDN docs).
To find out what objects depend on a given table, you could use something like:
SELECT
id,
OBJECT_NAME(ID)
FROM sys.sysdepends
WHERE depid = OBJECT_ID('YourTable')
That should give you a list of all objects depending on that table (or view or whatever you're checking)

Search inside objects (Sprocs, FUnctions, etc)

I am currently using sys.syscomments to locate objects where a certain parameter exists. Is there another method / process for doing this that would make it easier to find the objects that contain that parameter or term?
Thanks,
S
It would be preferable to use the definition column of sys.sql_modules instead. sys.syscomments text is nvarchar(4000) so you can have issues with truncation when a definition splits across multiple rows.
select quotename(s.name)+'.'+quotename(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
inner join sys.schemas s
on o.schema_id = s.schema_id
where m.definition like '%YourSearchText%'
Also, if you're not doing this programmatically, Red Gate offers a free plug-in for SSMS called SQL Search; I'm not sure if there are other tools available.

Resources