搜索prrem定义以获取未删除的表引用

时间:2016-02-16 17:14:31

标签: sql-server

所以,我一直在尝试搜索所有存储过程以获取对特定视图的任何引用;我可以这样做:

SELECT OBJECT_NAME(object_id), *
FROM sys.sql_modules
WHERE definition LIKE '%THEVIEW%'

唯一的问题是我不想手动通过所有这些来查看哪些实际上引用了视图,哪些已经注释掉了。有没有人有这样做的好方法?我的想法是可能有一个可以处理它的软件工具(我假设你不能通过SQL来实现)

由于

2 个答案:

答案 0 :(得分:2)

您可以使用以下查询来查找所有依赖项

这表明对象依赖于db.oMyView

SELECT
  coalesce(Referenced_server_name+'.','')+ --possible server name if cross-server
       coalesce(referenced_database_name+'.','')+ --possible database name if cross-database
       coalesce(referenced_schema_name+'.','')+ --likely schema name
       coalesce(referenced_entity_name,'') + --very likely entity name
       coalesce('.'+col_name(referenced_ID,referenced_minor_id),'')AS [referencing],
coalesce(object_schema_name(Referencing_ID)+'.','')+ --likely schema name
    object_name(Referencing_ID)+ --definite entity name
    coalesce('.'+col_name(referencing_ID,referencing_minor_id),'') AS [referenced]
FROM sys.sql_expression_dependencies
WHERE referenced_id =object_id('db.oMyView')
ORDER BY [referenced]

这显示了db.oMyView依赖的对象

SELECT
  coalesce(object_schema_name(Referencing_ID)+'.','')+ --likely schema name
    object_name(Referencing_ID)+ --definite entity name
    coalesce('.'+col_name(referencing_ID,referencing_minor_id),'')
       AS [referencing],
  coalesce(Referenced_server_name+'.','')+ --possible server name if cross-server
       coalesce(referenced_database_name+'.','')+ --possible database name if cross-database
       coalesce(referenced_schema_name+'.','')+ --likely schema name
       coalesce(referenced_entity_name,'') + --very likely entity name
       coalesce('.'+col_name(referenced_ID,referenced_minor_id),'')AS [referenced]
FROM sys.sql_expression_dependencies
WHERE referencing_id =object_id('db.oMyView')
ORDER BY [referenced]

来源:Dependencies and References in SQL Server

另一个参考:Different Ways to Find SQL Server Object Dependencies

答案 1 :(得分:2)

sp_Depends也会做到这一点..

create view vw_test
as
select 1 as 'b'

create proc usp_testvw
as
begin
select * from vw_test
end

create proc usp_testvw1
as
begin
select * from vw_test
end

现在传递您要检查的视图..

sp_depends 'vw_test'

输出:

name              type
dbo.usp_testvw    stored procedure
dbo.usp_testvw1   stored procedure