sql - sys.sql_dependency 目录 View 未更新会产生什么后果

标签 sql sql-server sql-server-2005

如果我有两个简单的存储过程,如下创建:

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 上没有记录任何依赖关系。

我的问题有两个:

  1. 这会产生什么后果?
  2. 一切似乎都可以很好地结合在一起,那么为什么 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/

相关文章:

sql - 计算给定时间段内有多少首条目和末尾条目相等

asp.net - 为什么在 Web 应用程序中由 Reporting Services 2005 调用时,第二个 T-SQL 查询的运行速度比第一个快得多

sql-server - 不带端口的 SQL Server 命名实例远程连接

sql-server - .net MVC4 与 SQL Server 2005/2008 的接口(interface)

sql-server - 将 SQL Server 命名实例更改为默认实例

sql - 将 SQL Server 数据库转换为 MYSQL 数据库

sql - 用于在 SQL Server 2005 中更新存储过程中的表的 if-else 条件

java - 使用 Java 从 SQL 生成 Liquibase 更改

mysql - 如何在sql中合并计数值

sql-server - SQL 服务器 2008 R2 : Dynamic query for pivot table with where and having clause