How to identify all stored procedures referring a particular table - sql

I created a table on development environment for testing purpose and there are few sp's which are refreing this table. Now I have have to drop this table as well as identify all sp's which are referring this table. I am facing difficulty to find list of all sp's. Please suggest some query by assuming that the table name is 'x' and database is sql server 2005.

SELECT Name
FROM sys.procedures
WHERE OBJECT_DEFINITION(OBJECT_ID) LIKE '%TableNameOrWhatever%'
BTW -- here is a handy resource for this type of question: Querying the SQL Server System Catalog FAQ

The following works on SQL2008 and above. Provides a list of both stored procedures and functions.
select distinct [Table Name] = o.Name, [Found In] = sp.Name, sp.type_desc
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', 'FN')
where o.name = 'YourTableName'
order by sp.Name

sometimes above queries will not give correct result, there is built in stored procedure available to get the table dependencies as:
EXEC sp_depends #objname = N'TableName';

A non-query way would be to use the Sql Server Management Studio.
Locate the table, right click and choose "View dependencies".
EDIT
But, as the commenters said, it is not very reliable.

In management studio you can just right click to table and click to 'View Dependencies'
than you can see a list of Objects that have dependencies with your table :

The following query will fetch all Stored Procedure names and the corresponding definition of those SP's
select
so.name,
text
from
sysobjects so,
syscomments sc
where
so.id = sc.id
and UPPER(text) like '%<TABLE NAME>%'

SELECT
o.name
FROM
sys.sql_modules sm
INNER JOIN sys.objects o ON
o.object_id = sm.object_id
WHERE
sm.definition LIKE '%<table name>%'
Just keep in mind that this will also turn up SPs where the table name is in the comments or where the table name is a substring of another table name that is being used. For example, if you have tables named "test" and "test_2" and you try to search for SPs with "test" then you'll get results for both.

The query below works only when searching for dependencies on a table and not those on a column:
EXEC sp_depends #objname = N'TableName';
However, the following query is the best option if you want to search for all sorts of dependencies, it does not miss any thing. It actually gives more information than required.
select distinct
so.name
--, text
from
sysobjects so,
syscomments sc
where
so.id = sc.id
and lower(text) like '%organizationtypeid%'
order by so.name

SELECT DISTINCT OBJECT_NAME(OBJECT_ID),
object_definition(OBJECT_ID)
FROM sys.Procedures
WHERE object_definition(OBJECT_ID) LIKE '%' + 'table_name' + '%'
GO
This will work if you have to mention the table name.

You have basically 2 options:
----Option 1
SELECT DISTINCT so.name
FROM syscomments sc
INNER JOIN sysobjects so ON sc.id=so.id
WHERE sc.TEXT LIKE '%tablename%'
----Option 2
SELECT DISTINCT o.name, o.xtype
FROM syscomments c
INNER JOIN sysobjects o ON c.id=o.id
WHERE c.TEXT LIKE '%tablename%'
These 2 queries will get you all the stored procedures that are referring the table you desire. This query relies on 2 sys tables which are sysobjects and syscomments. The sysobjects is where all of your DB object names are stored this includes the stored procedures.
The syscomments contains the text for all of your procedures.
If you query:
SELECT * FROM syscomments
You'll have a table containing the id which is the mapping to the sysobjects table with the text contained in the stored procedures as the last column.

This useful query also works if you are using Azure SQL / Synapse Anlaytics
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 '%Table_Name%'

Try This
SELECT DISTINCT so.name
FROM syscomments sc
INNER JOIN sysobjects so ON sc.id=so.id
WHERE sc.TEXT LIKE '%your table name%'

SELECT DISTINCT o.name, o.xtype
FROM syscomments c
INNER JOIN sysobjects o ON c.id=o.id
WHERE c.TEXT LIKE '%tablename%'

Related

Fetching of the Columns and Rows using Database name over Schema Name

I am trying to achieve finding the number of rows in a given column, I understand that we can achieve that by doing something like
SELECT DISTINCT s.name as SchemaName,OBJECT_NAME(o.OBJECT_ID) AS TableName,p.row_count
FROM
SYS.objects o JOIN SYS.schemas s
ON o.schema_id=s.schema_id
JOIN sys.dm_db_partition_stats p
ON o.object_id=p.object_id
WHERE o.type LIKE 'U'
AND s.name LIKE '%dbo%' -- schema name
ORDER BY TableName
However I could not find a join between the Sys.Databases and the aforementioned query. Suggestions? Or is there any efficient way to achieve the same?

SSMS Run a specific Query in SQL Query Editor Based on Highlighted Table / View

When writing SQL, I constantly find myself checking the structure of a table / view or selecting the top 100 rows to get a preview of the data.
For instance, when I have something like this (and I have forgotten the structure of table2)...
SELECT
FROM table1 INNER JOIN table2 ON table1.fieldX = table2.???
I would like to highlight a table / view name and then pass it to a SQL query by passing a parameter, say table2 to either of the following 2 queries
SELECT TOP 100 * FROM table2
or
SELECT o.Type, o.Type_Desc, o.name AS ObjectName, c.name AS ColumnName
FROM sys.objects AS o INNER JOIN sys.columns AS c ON o.object_id = c.object_id
WHERE o.name = N'table2' AND o.Type IN (N'U', N'V')
ORDER BY o.name,c.column_id,c.name
I use SSMS Boost (it's my favorite SSMS addin) but can't figure out how to use functionality (Auto Replacement, Shortcut, Macros) in order to accomplish what I want here.
Has anyone ever set this up?

How do I find the which table it is belong when I'm only given column name?

Hi I am working on SQL query, a total novice.
So I only have column name, and trying to find which table it is belong to.
How do I find that in toad? Can someone help?
Most databases have tables that describe the contents of the databases. If you are using toad, then I might surmise that you are using Oracle.
If so, you can use:
select *
from syscolumns
where columnname = <whatever you are looking for>
And then lookup the referenceid in systables.
In many other databases, you can use:
select *
from INFORMATION_SCHEMA.columns
where column_name = <whatever you are looking for>
Something like this should work, it will return the column name you search and the associated table
SELECT c.name as columnname, t.name as tablename from sys.columns c
join sys.tables t on c.object_id = T.object_id
where c.name =' put the column you want to find here'

How do I check if one table data is being referred in other processes

How do I check if one table data is being referred in other processes / procedure / is being used in other tables in sybase?
Try this
SELECT DISTINCT o.name, o.xtype
FROM dbo.syscomments c
INNER JOIN dbo.sysobjects o ON c.id=o.id
WHERE c.TEXT LIKE 'Table_Name_here'
-- and xtype = 'P' --un-comment if you want only procedures

Columns used in stored procedure

How to find if a column is used in any insert, update or delete statements in a stored procedure. I used a system query, but was not accurate. Can we check it manually in sql management studio or any query is there?
The easier way is to use RedGate SQL Search.
But, you can build a query using INFORMATION_SCHEMA.ROUTINES and the ROUTINE_DEFINITION if you feel like.
edit:
You can use this
SELECT ROUTINE_NAME, ROUTINE_DEFINITION
FROM INFORMATION_SCHEMA.ROUTINES
WHERE ROUTINE_DEFINITION LIKE '%YOUR COLUMN %'
AND ROUTINE_TYPE='PROCEDURE'
or if you have more than one column with same name in database, you can use this.
select DISTINCT p.name
from sys.procedures p
INNER JOIN sys.sql_dependencies d on p.object_id = d.object_id
INNER JOIN sys.tables t on t.object_id = d.referenced_major_id
INNER JOIN INFORMATION_SCHEMA.ROUTINES R ON R.ROUTINE_NAME = p.name
WHERE R.ROUTINE_DEFINITION like '%YOUR COLUMN %' AND t.name = YOURTABLE
But, I think you should use Search SQL