查找引用多个特定表的存储过程

时间:2015-02-03 15:54:33

标签: sql sql-server stored-procedures

这是我正在使用的SQL:

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 = 'PacManPackage' and o.name = 'PacManCompanyPackage'
order by 
    sp.Name

如果我删除只有一个表的o.name和视图就可以了。但我需要找到引用几个不同表的存储过程(但它们必须引用所有这些表)

1 个答案:

答案 0 :(得分:2)

如果我理解正确,你想找到引用所有表格的存储过程或标量函数吗?这个快速而肮脏的答案怎么样;

select
    sp.name
from
    sys.objects sp
        inner join sys.sql_expression_dependencies sed on sed.referencing_id = sp.object_id
            inner join sys.objects t on t.object_id = sed.referenced_id
                                    and t.type = 'U'

where 
    sp.type in ('P', 'FN')
    and t.name in ('PacManPackage', 'PacManCompanyPackage')
group by
    sp.name
having 
    count(*) = 2 -- change accordingly