有没有办法找到哪些存储过程在表中创建条目。比如说:
存储过程A插入表A中 存储的Proceudre B插入表A中 存储过程C插入表B
我希望查询返回存储过程A和存储过程B的名称。
我现在已经做到了,但它所做的就是找到存储过程。我认为找到存储过程是一个很好的起点。
select schema_name(schema_id) as [schema],
name
from sys.procedures
where name like '%Item%' and name like '%Create%'
我正在使用Microsoft SQL 2008
答案 0 :(得分:11)
您可以搜索包含所有过程和视图文本的sys.sql_modules
:
SELECT OBJECT_NAME(object_id)
FROM sys.sql_modules
WHERE definition LIKE '%TablenameIWanttoFind%'
如果您确定的措辞可以搜索'INSERT INTO mytable'
答案 1 :(得分:6)
该问题询问如何找到插入到特定表中的存储过程。
如果为多个选择引用了表,则搜索包含该名称的存储过程可能会带来很多误报。
sys.sql_dependencies
已弃用,但在此处非常有用,因为它包含is_updated
标记,对于插入也会设置为1
。
SELECT QUOTENAME(OBJECT_SCHEMA_NAME(object_id)) + '.' + QUOTENAME(OBJECT_NAME(object_id))
FROM sys.sql_dependencies
WHERE referenced_major_id = OBJECT_ID('YourTable')
AND is_updated = 1
GROUP BY object_id
答案 2 :(得分:2)
为什么不使用以下查询。
select O.name from sysobjects O
Join Syscomments C
on O.id=C.id
Where C.text like '%insert into%Table_name%'
从这个查询中你可以找到任何带有特定文本的程序。
答案 3 :(得分:1)
我需要知道实际的对象ID是什么,即存储过程/触发器等。
因此,感谢JNK,我更改了脚本以向我展示,如下所示:
SELECT OBJECT_NAME(object_id) name
,case xtype when 'AF' then 'Aggregate Function (CLR)'
when 'C' then 'CHECK Constraint'
when 'D' then 'Default or DEFAULT Constraint'
when 'F' then 'FOREIGN KEY Constraint'
when 'L' then 'Log'
when 'FN' then 'Scalar Function'
when 'FS' then 'Assembly (CLR) scalar-Function'
when 'FT' then 'Assembly (CLR) Table-valued Function'
when 'IF' then 'In-lined Table-Function'
when 'IT' then 'Internal Table'
when 'P' then 'Stored Procedure'
when 'PC' then 'Assembly (CLR) Stored-Procedure'
when 'PK' then 'PRIMARY KEY Constraint (Type is K)'
when 'RF' then 'Replication filter Stored Procedure'
when 'S' then 'System Table'
when 'SN' then 'Synonym'
when 'SQ' then 'Service Queue'
when 'TA' then 'Assembly (CLR) DML Trigger'
when 'TF' then 'Table Function'
when 'TR' then 'SQL DML Trigger'
when 'TT' then 'Table Type'
when 'U' then 'User Table'
when 'UQ' then 'UNIQUE Constraint (Type is K)'
when 'V' then 'View'
when 'X' then 'Extended Stored Procedure'
else '' end [xtype description]
FROM sys.sql_modules, sysobjects
where sql_modules.object_id = sysobjects.id
and definition LIKE '%InvNum%'
order by xtype,OBJECT_NAME(object_id)
结果显示如下:
答案 4 :(得分:0)
已执行列表的前10个存储过程,这些存储过程会在上次执行时间之前对表进行插入/更新,您可以在以下脚本中使用
GRANT VIEW SERVER STATE TO databaseuser
Select top 10 dest.objectid, DB_Name(dest.[dbid]) As 'databaseName'
, Object_Name(dest.objectid, dest.[dbid]) As 'procName'
, Max(deqs.last_execution_time) As 'last_execution'
From sys.dm_exec_query_stats As deqs
Cross Apply sys.dm_exec_sql_text(deqs.sql_handle) As dest
Join sys.sql_dependencies as sqldep on sqldep.object_id = dest.objectid
Where dest.[text] Like '%CashProduct%' -- replace
And dest.[dbid] Is Not Null -- exclude ad-hocs
And DB_Name(dest.[dbid]) = 'DatabaNameDu'
And sqldep.is_updated = 1
Group By db_name(dest.[dbid])
, Object_Name(dest.objectid, dest.[dbid]),
dest.objectid
Order By
Max(deqs.last_execution_time) desc
Option (MaxDop 1);