我想要一个脚本,该脚本将显示数据库中存储过程的依赖性。实际上,当我们手动进行视图依赖时,我将花费大量时间来拥有500多个存储过程。因此,我想知道这些存储过程是否在数据库中使用,以便删除无用的存储过程。
sp_depends
没有显示所有结果,因为我希望所有依赖此存储过程'usp_Constant_Get_Pvt'的对象及其依赖的对象。
EXEC sp_depends @objname = N'usp_Constant_Get_Pvt'
答案 0 :(得分:2)
我在类似情况下使用此脚本。不要忘记输入架构名称。
--
DECLARE
@sp nvarchar(100)
SET @sp = N'dbo.usp_Constant_Get_Pvt'
-- Objects that depends on [@sp]
SELECT
referencing_schema_name,
referencing_entity_name
FROM sys.dm_sql_referencing_entities(@sp, 'OBJECT')
-- Objects on which [@sp] depends
SELECT
referenced_schema_name,
referenced_entity_name
FROM sys.dm_sql_referenced_entities(@sp, 'OBJECT')
SELECT
referenced_schema_name,
referenced_entity_name
FROM sys.sql_expression_dependencies
WHERE referencing_id = OBJECT_ID(@sp)
答案 1 :(得分:1)
尝试以下查询
SELECT
referencing_schema_name,
referencing_entity_name,
referencing_id,
referencing_class_desc,
is_caller_dependent
FROM sys.dm_sql_referencing_entities ('YourObject', 'OBJECT');
请参考This link了解更多详细信息
答案 2 :(得分:0)
下面的代码将使用表达式依赖关系来查找存储过程的依赖关系。表达式依赖项的优点是,他们还可以找到跨数据库的依赖项。但是,如果有动态SQL,则必须在sql_modules
中进行搜索。
SELECT OBJECT_SCHEMA_NAME ( referencing_id ) AS referencing_schema_name,
OBJECT_NAME(referencing_id) AS referencing_entity_name,
o.type_desc AS referencing_desciption,
COALESCE(COL_NAME(referencing_id, referencing_minor_id), '(n/a)') AS referencing_minor_id,
referencing_class_desc, referenced_class_desc,
referenced_server_name, referenced_database_name, referenced_schema_name,
referenced_entity_name,
COALESCE(COL_NAME(referenced_id, referenced_minor_id), '(n/a)') AS referenced_column_name,
is_caller_dependent, is_ambiguous
FROM sys.sql_expression_dependencies AS sed
INNER JOIN sys.objects AS o ON sed.referencing_id = o.object_id
WHERE referenced_id = OBJECT_ID(N'SchemaName.StoredProcedureName');
GO
SELECT OBJECT_NAME(referencing_id) AS referencing_entity_name,
o.type_desc AS referencing_desciption,
COALESCE(COL_NAME(referencing_id, referencing_minor_id), '(n/a)') AS referencing_minor_id,
referencing_class_desc,
referenced_server_name, referenced_database_name, referenced_schema_name,
referenced_entity_name,
COALESCE(COL_NAME(referenced_id, referenced_minor_id), '(n/a)') AS referenced_column_name,
is_caller_dependent, is_ambiguous
FROM sys.sql_expression_dependencies AS sed
INNER JOIN sys.objects AS o ON sed.referencing_id = o.object_id
WHERE referencing_id = OBJECT_ID(N'SchemaName.StoredProcedureName');
GO