如果我有两个简单的存储过程,如下创建:
create procedure root as
select 1
go
create procedure dependant as
exec root
go
(其中dependant
依赖于root
)。
当我检查第二个过程的 sys.sql_dependency
表时,我看到一个条目(正如我所期望的那样)。
但是,如果我首先添加dependant
过程,则会收到以下警告。
Cannot add rows to sys.sql_dependencies for the stored procedure because it depends on the missing table 'root'. The stored procedure will still be created; however, it cannot be successfully executed until the table exists.
而且,没错,exec dependent;
失败了。
因此,当我添加 root
过程时,exec dependent;
可以工作,但是,sys.sql_dependency
上没有记录任何依赖关系。
我的问题有两个:
- 这会产生什么后果?
- 一切似乎都可以很好地结合在一起,那么为什么 SQL 不追溯添加此记录呢?
一如既往地提供帮助,我们非常感激。
最佳答案
结果是,当您重构数据库时,识别受影响的对象变得更加困难。
您可以通过编写脚本在所有数据库对象上运行 sp_refreshsqlmodule
来刷新所有依赖项。
在 SQL Server 2008 中,此类 Unresolved 依赖关系仍然被存储,并且可以通过 sys.sql_expression_dependencies
访问,这意味着依赖关系信息更加可靠。
SQL Server 2008 行为如下。
创建依赖项之后但在根存在之前
SELECT OBJECT_NAME(referencing_id) AS Name,
referencing_class_desc,
referenced_class_desc,
referenced_entity_name,
referenced_id,
is_caller_dependent,
is_ambiguous
FROM sys.sql_expression_dependencies
返回
Name referenced_entity_name referenced_id is_caller_dependent is_ambiguous
---------- ------------------------- ------------- ------------------- ------------
dependant root NULL 1 0
在您的示例代码中,这也是创建 root
之后的查询结果,因为对它的引用不是架构限定的,因此依赖于调用者。但是,如果您的 dependant
过程的定义更改为
create procedure dependant as
exec dbo.root
然后,一旦创建 dbo.root
,就会返回以下内容
Name referenced_entity_name referenced_id is_caller_dependent is_ambiguous
---------- ------------------------- ------------- ------------------- ------------
dependant root 2121058592 0 0
关于sql - sys.sql_dependency 目录 View 未更新会产生什么后果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3717628/