How to get the TARGET tables from a stored procedure - sql

I am aware of this query that can pull referenced tables from a SQL Server stored procedure.
For example, if I have this stored procedure:
UPDATE tbl1
SET symbol = tbl2.symbol, symbol2 = tbl2.symbol2
FROM tbl1
JOIN tbl2 ON tbl1.PK = tbl2.PK
This query:
SELECT Name
FROM sys.procedures
WHERE OBJECT_DEFINITION(OBJECT_ID) LIKE '%TableNameOrWhatever%'
and this one:
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>%'
both return tbl1 & tbl2 as the response.
My question is this: tb11 is ReadWrite as the SQL shows, and tbl2 is ReadOnly.
How do I update my queries to make the distinction?
So my output should be:
Response
========
tbl1, ReadWrite
tbl2, ReadOnly
Thanks!

Try sys.sql_dependencies, eg
select object_name(d.referenced_major_id) referenced_object,
object_name(d.object_id) referenced_by,
max(cast(d.is_updated as int)) is_updated
from sys.sql_dependencies d
join sys.objects o
on d.object_id = o.object_id
where o.type_desc = 'SQL_STORED_PROCEDURE'
group by d.referenced_major_id, d.object_id
order by d.object_id
But beware that procedures with deferred name resolution and stored procedures that access tables with dynamic SQL won't be tracked

The DMV sys.dm_sql_referenced_entities contains this information.
The following query should get you exactly what you need
SELECT
name,
referenced_entity_name,
is_updated
FROM sys.procedures p
CROSS APPLY sys.dm_sql_referenced_entities(SCHEMA_NAME(p.schema_id) + '.' + p.name,'OBJECT') r
WHERE referenced_minor_name IS NULL;
db<>fiddle

Related

How to find UserDefined Objects that reference System tables/functions in SQL Server

I have several User Defined Stored Procedures and functions that reference system tables and functions, my purpose is to find such resources and delete them,
I used the below query for one of the SP that I knew has system objects in it :
SELECT d.object_id,
d.referenced_major_id,
o.name,
o1.name
FROM sys.sql_dependencies d
JOIN sys.objects o ON o.object_id = d.referenced_major_id
JOIN sys.objects o1 ON o1.object_id = d.object_id AND d.object_id =
'ID_Of_UserDefinedSP_That_refers_system_Tables'
But it does not returns any system referenced objects, other functions that I tried :
sys.dm_sql_referencing_entities
sys.dm_sql_referenced_entities
It would be great if someone has a query to find all the UserDefined functions/Views/StoredProcedures(SP) that reference System tables/functions
The following query returns view type dependency information, stored procedures, functions, and connections to entity classes.
SELECT
*
FROM sys.sql_expression_dependencies AS sd
INNER JOIN sys.objects AS o
ON o.object_id = sd.referencing_id
get all procedure
Select * from sysobjects where type = 'p'
get all view
Select * from sysobjects where type = 'v'
get all function
SELECT
name AS 'Function Name',
SCHEMA_NAME(schema_id) AS 'Schema',
type_desc AS 'Function Type',
create_date AS 'Created Date'
FROM
sys.objects
WHERE
type_desc LIKE '%FUNCTION%';
get all procedure with dependencies
SELECT
*
FROM sys.sql_expression_dependencies AS sd
INNER JOIN sys.objects AS o
ON o.object_id = sd.referencing_id where type = 'p'

Trying to get the triggers associated with the table with a different schema name

I am trying to get the triggers of the table using this SQL statement:
SELECT c.text
FROM dbo.sysobjects s, dbo.syscomments c
WHERE s.name = 'redgate.tablecampare'
AND s.id = c.id
This works for dbo but not for different schema names
Assuming you're on SQL Server 2005 or greater, this should work:
select m.definition
from sys.triggers as t
join sys.sql_modules as m
on m.object_id = t.object_id
where t.parent_id = object_id('schema.object')
You can use below system catalog to find out triggers containing a specific table.
SELECT OBJECT_NAME(id)
FROM SYSCOMMENTS
WHERE [text] LIKE '%redgate.tablecampare%'
AND OBJECTPROPERTY(id, 'IsTrigger') = 1
GROUP BY OBJECT_NAME(id)
SELECT OBJECT_NAME(object_id)
FROM sys.sql_modules
WHERE OBJECTPROPERTY(object_id, 'IsTrigger') = 1
AND definition LIKE '%redgate.tablecampare%'

How to fetch tables and their corresponding column names used in stored procedures using t-sql?

I just want to get the tables and their corresponding columns which are written as a part of stored procedure using t-sql? How can I get a list of the same? Can anyone help me? Thanks.
If you have table names with you, use following query to get the required details as:
SELECT COLUMN_NAME,DATA_TYPE,CHARACTER_MAXIMUM_LENGTH,IS_NULLABLE,COLUMN_DEFAULT,TABLE_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME IN ('TBL1','TBL2') ORDER BY TABLE_NAME
I tried following effort to get my query resolved.
SELECT distinct sp.name as StoredProc, tbl.name AS [Table], col.name AS [Column]
FROM sysobjects sp
INNER JOIN sysdepends sd ON sp.id = sd.id
INNER JOIN sysobjects tbl ON tbl.id = sd.depid
INNER JOIN syscolumns col ON col.colid = sd.depnumber AND col.id = sd.depid
WHERE sp.name IN ( SELECT name FROM sysobjects WHERE id IN(SELECT id from syscomments
WHERE text LIKE '%passParamToGetYourSPs_%'))
--group BY sp.name
--AND sp.name = 'Search_SP_Name'
ORDER BY sp.name, tbl.name, col.name
Thanks for all your efforts. Smile | :)

T-SQL: Show stored procedures related to tables, cyclically

I'm using the following t-sql code:
USE [my_database]
SELECT DISTINCT so.name
FROM syscomments sc
INNER JOIN sysobjects so ON sc.id=so.id
WHERE sc.TEXT LIKE '%table_name%'
in order to show all the Stored Procedures that use the table table_name.
I want do this work for all tables in my database.
How can I perform this task and organize the output?
This uses information schema for both tables, and stored procedures. You can change or get rid of ROUTINE_TYPE condition to add functions, and you can change table type to return views.
This answer produces its results by checking what tables a stored procedure depends on. I think this will be a much more accurate result then checking if a name is in the query text. If the procedure refers to a table in a comment section, then this result will not be returned in the first query, but will be in the second and other answers given.
SELECT t.TABLE_NAME, s.ROUTINE_NAME
FROM INFORMATION_SCHEMA.TABLES t
INNER JOIN INFORMATION_SCHEMA.ROUTINES s ON
s.ROUTINE_NAME IN (SELECT referencing_entity_name
FROM sys.dm_sql_referencing_entities(TABLE_SCHEMA + '.' + TABLE_NAME, 'OBJECT'))
AND s.ROUTINE_TYPE = 'PROCEDURE'
WHERE t.TABLE_TYPE = 'BASE TABLE'
edit: Here's how to get the dependencies without the function. (I like this method the best)
SELECT DISTINCT t.name [TableName], p.name [ProcedureName]
FROM sys.objects t
LEFT JOIN sys.sql_dependencies d ON
d.referenced_major_id = t.object_id
LEFT JOIN sys.objects p ON
p.object_id = d.object_id
AND p.type = 'p'
WHERE t.type = 'u'
If your specific use is to just find any string that matches a table name, below will work:
SELECT t.TABLE_NAME, s.ROUTINE_NAME
FROM INFORMATION_SCHEMA.TABLES t
INNER JOIN INFORMATION_SCHEMA.ROUTINES s
ON CHARINDEX(t.TABLE_NAME, s.ROUTINE_DEFINITION) > 0
AND s.ROUTINE_TYPE = 'PROCEDURE'
WHERE t.TABLE_TYPE = 'BASE TABLE'
You could do a JOIN on LIKE:
select * from INFORMATION_SCHEMA.TABLES t
join
(
SELECT DISTINCT so.name
FROM syscomments sc
INNER JOIN sysobjects so ON sc.id=so.id
) x on x.name like '%' + t.TABLE_NAME + '%'
Note that your query doesn't restrict to procs - you'll also get views, defaults, and other objects too. If you just want procs, you can add where so.xtype = 'P' to your inner query.
Another version that uses sys tables only:
select t.name as TableName, p.name as SPName
from sys.objects t
join sys.syscomments c
on c.text like '%' + t.name + '%'
join sys.objects p
on p.object_id = c.id
where t.type = 'U' -- user table
and p.type = 'P' -- procedure
You can also use the built in function that's been around at least since SQL 2005 and works for tables, views, and stored procedures. I get the same number of results as Daniel's answer above when checking dependencies on a table in a fairly enterprisy database.
sp_depends [TableName]
sp_depends [TableName.Column]
sp_depends [StoredProcedureName]
http://msdn.microsoft.com/en-us/library/ms189487(v=sql.90).aspx

List of Stored Procedure from Table

I have a huge database with 100's of tables and stored procedures. Using SQL Server 2005, how can I get a list of stored procedures that are doing an insert or update operation on a given table.
sys.sql_dependencies has a list of entities with dependencies, including tables and columns that a sproc includes in queries. See this post for an example of a query that gets out dependencies. The code snippet below will get a list of table/column dependencies by stored procedure
select sp.name as sproc_name
,t.name as table_name
,c.name as column_name
from sys.sql_dependencies d
join sys.objects t
on t.object_id = d.referenced_major_id
join sys.objects sp
on sp.object_id = d.object_id
join sys.columns c
on c.object_id = t.object_id
and c.column_id = d.referenced_minor_id
where sp.type = 'P'
select
so.name,
sc.text
from
sysobjects so inner join syscomments sc on so.id = sc.id
where
sc.text like '%INSERT INTO xyz%'
or sc.text like '%UPDATE xyz%'
This will give you a list of all stored procedure contents with INSERT or UPDATE in them for a particular table (you can obviously tweak the query to suit). Also longer procedures will be broken across multiple rows in the returned recordset so you may need to do a bit of manual sifting through the results.
Edit: Tweaked query to return SP name as well. Also, note the above query will return any UDFs as well as SPs.
Use sys.dm_sql_referencing_entities
Note that sp_depends is obsoleted.
MSDN Reference
You could try exporting all of your stored procedures into a text file and then use a simple search.
A more advanced technique would be to use a regexp search to find all SELECT FROM and INSERT FROM entries.
If you download sp_search_code from Vyaskn's website it will allow you to find any text within your database objects.
http://vyaskn.tripod.com/sql_server_search_stored_procedure_code.htm
This seems to work:
select
so.name as [proc],
so2.name as [table],
sd.is_updated
from sysobjects so
inner join sys.sql_dependencies sd on so.id = sd.object_id
inner join sysobjects so2 on sd.referenced_major_id = so2.id
where so.xtype = 'p' -- procedure
and is_updated = 1 -- proc updates table, or at least, I think that's what this means
SELECT Distinct SO.Name
FROM sysobjects SO (NOLOCK)
INNER JOIN syscomments SC (NOLOCK) on SO.Id = SC.ID
AND SO.Type = 'P'
AND (SC.Text LIKE '%UPDATE%' OR SC.Text LIKE '%INSERT%')
ORDER BY SO.Name
This link was used as a resource for the SP search.