您好,我是小DAI,专注于数据库管理员相关的技术问答,请问有什么可以帮您?

sp_depends

说明

显示有关数据库对象依赖性的信息,这些对象是数据库中依赖于指定的表或视图的视图、触发器、用户定义的函数、过程和谓词,数据库中指定的视图、触发器、过程或谓词所依赖的表和视图,以及与表有关的多个触发器。不能在视图中授予谓词。

还显示有关表列依赖性的信息,如果提供了 <column_name>,则这些对象是在指定的任一列中定义的索引、缺省值、检查约束、规则、预计算结果集、参照完整性约束和谓词;如果未提供 <column_name>,则这些对象是在表中所有列上定义的索引、缺省值、检查约束、规则、预计算结果集、参照完整性约束和谓词。

语法


sp_depends <objname>[, <column_name>]

参数

<objname>

是要对其进行依赖性检查的表、视图、Transact-SQL 存储过程、SQLJ 存储过程、SQLJ 函数或触发器的名称。您不能指定数据库名。如果对象所有者既不是运行该命令的用户也不是数据库所有者,则使用所有者名。

<column_name>

是要对其进行依赖性检查的列的名称。

示例

依赖于表的对象

列出依赖于表 sysobjects 的数据库对象:


sp_depends sysobjects

依赖于视图的对象

列出依赖于 titleview 视图的数据库对象,以及 titleview 视图所依赖的数据库对象:


sp_depends titleview

Things that the object references in the current database.
object          type        updated selected
--------------  ----------- ------- -----
dbo.authors     user table  no      no
dbo.titleauthor user table  no      no
dbo.titles      user table  no      no
Things inside the current database that reference the object.
object          type 
------------    --------------- 
dbo.tview2      view

依赖于特定表的对象

列出依赖于用户“mary”所拥有的 titles 表的数据库对象。因为句点是特殊字符,所以需要加引号:


sp_depends "mary.titles"

⚠️

数据库用户(而不是执行该命令的用户)和数据库所有者拥有的对象必须用所有者的名称进行限定,如此示例中所示。

预计算结果集

以下示例假定 prs1 和 view1 是按下列依赖结构创建的:

prs1 可在基表 tab1(c1 列具有唯一约束)上定义,view1 可在 prs1 上定义

prs1 已配置为使用 immediate 刷新

本例显示包括 c1 列依赖性的预计算结果集:


sp_depends prs1,c1

Things the object references in the current database.
object       type           updated    selected         
------------ -------------- ---------- --------- 
dbo.tab1     user table     no         no               

Things inside the current database that reference the object.
object             type     
------------------ -------- 
dbo.view1          view     

Dependent objects that reference column c1.
Columns referenced in stored procedures, views or triggers are not 
included in this report.
Type  Property   Object Names or Column Names Also see/Use command                   
----- ---------- ---------------------------- -------------------- 
index constraint prs1_10240036482 (c1)        sp_helpindex,
                                              drop index,
                                              sp_helpconstraint, 
                                              alter table drop
                                              constraint

谓词和表之间的依赖性

显示谓词 pred1 与其引用的任意表之间的依赖性:


sp_depends pred1

Things the object references in the current database.
object           type                 updated        selected
---------------- -------------------- -------------- ----------------
dbo.tab1         user table           no             no
dbo.tab2         user table           no             no

谓词、表和列之间的依赖性

显示谓词与表 tab1 和列 col1 之间的依赖性:


sp_depends tab1, col1

Things inside the current database that reference the object.
object             type
------------------ ------------------
dbo.pred1          predicate
Dependent objects that reference column col1.
Columns referenced in stored procedures, views or triggers are not included in this report.
Type                 Property                  
    Object Names or Column Names
       Also see/Use command
-------------------- --------------------
    -----------------------------------------------------------------
       ------------------------------------------------------
permission           permission
    column permission
       sp_helprotect, grant/revoke

列级依赖性

显示 sysobjects 表的所有列的列级依赖性:


sp_depends sysobjects

Things inside the current database that reference the object.
object                                   type
---------------------------------------- ----------------
dbo.sp_dbupgrade                         stored procedure
dbo.sp_procxmode                         stored procedure

Dependent objects that reference all columns in the table. Use sp_depends
on each column to get more information.
Columns referenced in stored procedures, views or triggers are not included
in this report.

Column           Type         Object Names or Column Names
----------------------- ------------ ------------------------------
cache            permission   column permission
ckfirst          permission   column permission
crdate           permission   column permission
deltrig          permission   column permission
expdate          permission   column permission
id               index        sysobjects (id)
id               logical RI   From syscolumns (id) To sysobjects (id)
id               logical RI   From syscomments (id) To sysobjects (id)
id               logical RI   From sysdepends (id) To sysobjects (id)
id               logical RI   From sysindexes (id) To sysobjects (id)
id               logical RI   From syskeys (depid) To sysobjects (id)
id               logical RI   From syskeys (id) To sysobjects (id)
id               logical RI   From sysobjects (id) To sysprocedures (id)
id               logical RI   From sysobjects (id) To sysprotects (id)
id               logical RI   sysobjects (id)
id               permission   column permission
indexdel         permission   column permission
instrig          permission   column permission
loginame         permission   column permission
name             index        ncsysobjects (name, uid)
name             permission   column permission
objspare         permission   column permission
schemacnt        permission   column permission
seltrig          permission   column permission
sysstat          permission   column permission
sysstat2         permission   column permission
type             permission   column permission
uid              index        ncsysobjects (name, uid)
uid              logical RI   From sysobjects (uid) To sysusers (uid)
uid              permission   column permission
updtrig          permission   column permission
userstat         permission   column permission
versionts        permission   column permission

详细列级依赖性

显示有关 sysobjects 表中 id 列的更详细的列级依赖性信息:


sp_depends sysobjects, id

Things inside the current database that reference the object.
object                                             type
------------------------------------               -------------
dbo.sp_dbupgrade                                   stored procedure
dbo.sp_procxmode                                   stored procedure
Dependent objects that reference column id.
Columns referenced in stored procedures, views or triggers are not included 
in this report.
Type          Property     Object Names or Column Names
                           Also see/Use command
----------   ---------     ----------------------------------
                           ----------------------------------
index         index       sysobjects (id)
                          sp_helpindex, drop index,
                          sp_helpconstraint, alter table drop constraint
logical RI    primary     sysobjects (id)
                          sp_helpkey, sp_dropkey
logical RI    foreign     From syskeys (id) To sysobjects (id)
                          sp_helpkey, sp_dropkey
logical RI    common      From syscolumns (id) To sysobjects (id)
                          sp_helpkey, sp_dropkey
logical RI    common      From sysdepends (id) To sysobjects (id)
                          sp_helpkey, sp_dropkey
logical RI    common      From sysindexes (id) To sysobjects (id)
                          sp_helpkey, sp_dropkey
logical RI    common      From syskeys (depid) To sysobjects (id)
                          sp_helpkey, sp_dropkey
logical RI    common      From syscomments (id) To sysobjects (id)
                          sp_helpkey, sp_dropkey
logical RI    common      From sysobjects (id) To sysprotects (id)
                          sp_helpkey, sp_dropkey
logical RI    common      From sysobjects (id) To sysprocedures (id)
                          sp_helpkey, sp_dropkey
permission    permission  column permission
                          sp_helprotect, grant/revoke

所有列的列级依赖性

显示用户创建的表 titles 的所有列的列级依赖性:


1> sp_depends titlesThings inside the current database that reference the object.
object                                type
-----------------------------------  ---------------
dbo.deltitle                          trigger
dbo.history_proc                      stored procedure
dbo.title_proc                        stored procedure
dbo.titleid_proc                      stored procedure
dbo.titleview                         view
dbo.totalsales_trig                   trigger

Dependent objects that reference all columns in the table. Use sp_depends 
on each column to get more information.
Columns referenced in stored procedures, views or triggers are not included 
in this report.

Column     Type         Object Names or Column Names
------     -----        ----------------------------------------
pub_id     logical RI   From titles (pub_id) To publishers (pub_id)
pubdate    default      datedflt
title      index        titleind (title)
title      statistics   (title)
title_id   index        titleidind (title_id)
title_id   logical RI   From roysched (title_id) To titles (title_id)
title_id   logical RI   From salesdetail (title_id) To titles (title_id)
title_id   logical RI   From titleauthor (title_id) To titles (title_id)
title_id   logical RI   titles (title_id)
title_id   rule         title_idrule
title_id   statistics   (title_id)
type       default      typedflt

特定列的列级依赖性

显示有关用户创建的 titles 表中 pub_id 列的更详细的列级依赖性信息:


sp_depends titles, pub_id

Things inside the current database that reference the object.
object                                  type
------------------------------------    ----------------
dbo.deltitle                            trigger
dbo.history_proc                        stored procedure
dbo.title_proc                          stored procedure
dbo.titleid_proc                        stored procedure
dbo.titleview                           view
dbo.totalsales_trig                     trigger
Dependent objects that reference column pub_id.
Columns referenced in stored procedures, views or triggers are not
included in this report.
Type          Property    Object Names or Column Names
                          Also see/Use command
----------    ---------   ---------------------
                          ----------------------------
logical RI    foreign     From titles (pub_id) To publishers (pub_id)
                        sp_helpkey, sp_dropkey

用法

  • 执行 sp_depends 将列出当前数据库中依赖于 <objname> 以及 <objname> 所依赖的所有对象。例如,视图可以依赖于一个或多个表,同时过程或其它视图又可以依赖于这些视图。如果某个对象引用了另一个对象,则前者将依赖于后者。将不报告对当前数据库之外对象的引用。

  • 在修改或删除列之前,请使用 sp_depends 确定表是否包含可能受该修改影响的任何依赖对象。例如,如果修改列使用新数据类型,则可能需要重新定义与该表密切联系的对象,以与该列的新数据类型保持一致。

  • sp_depends 过程通过查看 sysdepends 表确定依赖性。

    如果不按顺序创建对象(例如,如果在创建视图之前就创建了使用该视图的过程),那么在 sysdepends 中就不存在有关依赖性的行,而且 sp_depends 也不报告这些依赖性。

  • 如果所报告的对象是存储过程或触发器,则 sp_depends 产生的报告中的 updated 和 selected 列是有意义的。updated 列的值指示存储过程是否更新该对象。selected 列指示该对象是否用于读游标或数据修改语句。

    sp_depends 遵循用于查找对象的 SAP ASE 规则:

  • 如果用户没有指定所有者名,并且执行该命令的用户拥有具有指定名称的对象,则使用该对象。

  • 如果用户没有指定所有者名,并且用户不拥有具有该名称的对象,但数据库所有者却拥有这样的对象,则使用数据库所有者的对象。

  • 如果用户和数据库所有者都不拥有具有该名称的对象,则即使数据库中存在具有此名称但属于其它所有者的对象,该命令也会报告一个错误状态。

  • 如果用户和数据库所有者都拥有具有指定名称的对象,并且用户想访问数据库所有者的对象,则必须指定名称,例如 <dbo.objectname>

    Java 方法

    SQLJ 函数和 SQLJ 存储过程是包装在 SQL 包装中的 Java 方法。

  • SQLJ 函数和 SQLJ 存储过程是可列出依赖性的数据库对象。SQLJ 存储过程和 SQLJ 函数的唯一依赖性是 Java 类。

  • 如果 <objname> 是 SQLJ 存储过程或 SQLJ 函数,则 sp_depends 可列出 create 语句中声明的例程外部名称中的 Java 类,而不是作为返回类型指定的类或参数列表中的数据类型。

  • SQLJ 存储过程和 SQLJ 函数可作为其它数据库对象的依赖性列出。