Find all stored procedures including a text in their body - sql-server

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

Related

How to find all tables depends to stored procedure

I want to find all tables used in stored procedures but it only gives me the tables that are in the database that I'm just using. Is there a solution to find all tables used in stored procedures, which are in other databases (in the same server)?
I try this:
SELECT DISTINCT p.name AS proc_name, t.name AS table_name
FROM sys.sql_dependencies d
INNER JOIN sys.procedures p ON p.object_id = d.object_id
INNER JOIN sys.tables t ON t.object_id = d.referenced_major_id
WHERE p.name like '%sp_example%'
ORDER BY proc_name, table_name
The procedures I need to analyze contain tables from different databases, but the code above gives me only results from one database.
First of all, sys.sql_dependencies is deprecated. Instead, you should use sys.sql_expression_dependencies. In it, you will find 4-part names of referenced objects (this includes objects referenced via linked server, for example).
Second, any object_id in SQL Server only makes sense within its database. If you are looking for anything outside of your current DB, don't join tables by these identifiers - use object names instead.

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]

How to check a SQL Server view dependencies

I need to modify a SQL Server view in a large database and need to know what other objects in this database use this view. How can I do that?
I tried viewing dependencies in SQL Server Management Studio but that does not list the objects that depend on this view (I know of at least one stored procedure that depends on this view, and it is not listed as a dependency).
The other option that I considered is scripting the whole database and doing the text search in it, but I want to see if there is an automated way to do this.
Try to query syscomments table and find the usage.
select o.name,o.type from sys.syscomments c
inner join sys.objects o
on o. object_id = c.id
where c.text like '%your view name%'
Update #1
select o.name,o.type from sys.sql_modules m
inner join sys.objects o
on o.object_id = m.object_id
where m.definition like '%your view name%'

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.

Using SQL Server - is there a way to query 'Create view' SQL

I want to know if a certain table dbo.person is being used in any of the database views in my database. There are a lot of views in this database.
I could right click each view "Script View as --> CREATE To" to see the SQL that was used to build this view but this will take a long time.
I am wondering if there is any way to query all of these "create view as" scripts to see if any of them mention my table dbo.person.
I hope this is clear.
You can query the system catalog for views (and use sql_modules to get the view definition) then use the system view sys.sql_expression_dependencies to find which of these views reference dbo.Person:
SELECT ViewName = QUOTENAME(OBJECT_SCHEMA_NAME(v.[object_id])) + '.' + QUOTENAME(v.Name),
m.[Definition]
FROM sys.views AS v
INNER JOIN sys.sql_modules AS m
ON m.[object_id] = v.[object_id]
WHERE EXISTS
( SELECT 1
FROM sys.sql_expression_dependencies AS d
WHERE d.Referenced_id = OBJECT_ID(N'dbo.Person', 'U')
AND v.[object_id] = d.referencing_id
)
ORDER BY ViewName;
There are some small issues with sys.sql_expression_dependencies, but I would still be more inclined to use this than to search for '%person%', since this could bring back 10s, or 100s of extra results, any time a table that contains person (e.g. dbo.PersonAddress) is referenced, or person is used as an alias (SELECT Forename + surname AS Person) etc. It really depends whether you are looking for something that is going to be accurate most of the time, but may, on occasion miss a reference, or whether you need a catch all solution that will bring back extra results.
You could try
select * from INFORMATION_SCHEMA.VIEWS
where VIEW_DEFINITION like '%Person%'
You might need to change the WHERE to dbo.person or [Person]
For a more detailed string search on SQL Server database object definitions please refer to SQL Object Where Used List script
You can simply call like
exec SearchInObjectsText 'name'
and it will return procedures, functions, views, etc where the text "name" is used
I know this doesn't answer the question directly, but it answers the need. I use a third party add-in called SQLSearch from redgate. It's free and I'm not an employee, so this isn't a "plug" of any kind.
I normally use this SQL to look in definitions. I didn't filter on VIEWS just in case it is used somewhere else.
SELECT
so.name, so.type, sm.Definition
FROM
sys.objects AS so INNER JOIN sys.sql_modules AS sm ON so.object_id = sm.object_id
WHERE
sm.Definition LIKE N'%dbo.person%'
ORDER BY so.name
Please query Information_Schema.View_Table system views
select * from INFORMATION_SCHEMA.VIEW_TABLE_USAGE where TABLE_NAME = 'Emp'
Try this
SELECT DISTINCT m.definition, 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 '%person%'
and type_desc = 'VIEW'

Resources