代码之家  ›  专栏  ›  技术社区  ›  Mutation Person

sys.sql_dependencies目录视图未更新的后果是什么?

  •  2
  • Mutation Person  · 技术社区  · 14 年前

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

    create procedure root as
    select 1 
    go
    
    create procedure dependant as
    exec root
    go
    

    (何处) dependant 取决于 root )

    当我检查 sys.sql_dependencies 表为第二个过程,我看到一个条目(如我所期望的)。

    但是,如果我添加 受抚养人 首先,我得到以下警告。

    无法将行添加到 存储的 程序,因为它取决于 缺少表“root”。存储的 程序仍将被创建; 但是,它不能成功 在表存在之前执行。

    而且,够了, exec dependant; 失败。

    所以,当我加入 程序, 执行依赖; 但是,不记录依赖项 sys.sql_依赖项 .

    我的问题是双重的:

    1. 这会造成什么后果?
    2. 所有的东西似乎都在一起,这是可以接受的,那么为什么SQL不回顾性地添加这个记录呢?

    一如既往地,我们非常感谢您的帮助。

    1 回复  |  直到 14 年前
        1
  •  4
  •   Martin Smith    14 年前

    结果就是,当您重构数据库时,更难识别受影响的对象。

    通过编写要运行的脚本,可以刷新所有依赖项 sp_refreshsqlmodule 在所有数据库对象上。

    在SQL Server 2008中,这些未解析的依赖项仍然被存储,并且可以通过 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