在数据库应用项目的生命周期中充分利用 DB2 目录视图的最佳实践
2010-07-12 00:00:00 来源:WEB开发网部署阶段的实践案例
实践三 : 部署前分析依赖关系
在数据库对象部署前,有经验的开发或管理人员会检查数据库对象之间的依赖关系。除了部署修改过定义(代码)的对象以外,还需要准备步骤去处理与修改过对象有依赖关系的其他对象。清单 3.显示了如何查询依赖于一个表的所有数据库对象,包括引用这个表的视图(从 SYSCAT.VIEWDEP),用户自定义函数(从 SYSCAT.FUNCDEP)和存储过程(从 SYSCAT.ROUTINEDEP 和 SYSCAT.PACKAGEDEP),如结果所示,其中还返回多层次关系(例如调用这个表的视图又被其他用户自定义函数调用)。
清单 3. 如何查询依赖于一个表的所有数据库对象
db2=> WITH OBJ as (
select TABSCHEMA as SCHEMA, TABNAME as NAME from SYSCAT.TABLES
where TABSCHEMA = upper('test')
and TABNAME = upper('customer')
),
PROC as (
select PROCSCHEMA, PROCNAME, rd.bname as PKGNAME
from SYSCAT.PROCEDURES p
join syscat.routinedep rd
on( p.PROCSCHEMA = rd.routineschema
and p.specificname = rd.routinename)
),
RPL (
ROOTTYPE, ROOTSCHEMA, ROOTNAME, LEVEL, SCHEMA, NAME, DTYPE,
BTYPE, BSCHEMA, BNAME
) AS (
SELECT
BTYPE AS ROOTTYPE, BSCHEMA AS ROOTSCHEMA, BNAME AS ROOTNAME,
1 as LEVEL, VIEWSCHEMA as SCHEMA, VIEWNAME as NAME,
DTYPE, BTYPE, BSCHEMA,
BNAME
FROM SYSCAT.VIEWDEP ROOT
UNION ALL
SELECT
ROOTTYPE, ROOTSCHEMA, ROOTNAME, PARENT.LEVEL+1,
CHILD.VIEWSCHEMA as SCHEMA,
CHILD.VIEWNAME as NAME, CHILD.DTYPE, CHILD.BTYPE,
CHILD.BSCHEMA, CHILD.BNAME
FROM RPL PARENT, SYSCAT.VIEWDEP CHILD
WHERE PARENT.SCHEMA = CHILD.BSCHEMA
AND PARENT.NAME = CHILD.BNAME
AND PARENT.LEVEL < 50
UNION ALL
SELECT
BTYPE AS ROOTTYPE, BSCHEMA AS ROOTSCHEMA, BNAME AS ROOTNAME,
1 as LEVEL, FUNCSCHEMA as SCHEMA, FUNCNAME as NAME,
'F' as DTYPE, BTYPE,
BSCHEMA, BNAME
FROM SYSCAT.FUNCDEP ROOT
UNION ALL
SELECT
ROOTTYPE, ROOTSCHEMA, ROOTNAME, PARENT.LEVEL+1,
CHILD.FUNCSCHEMA as SCHEMA,
CHILD.FUNCNAME as NAME, 'F' as DTYPE, CHILD.BTYPE,
CHILD.BSCHEMA, CHILD.BNAME
FROM RPL PARENT, SYSCAT.FUNCDEP CHILD
WHERE PARENT.SCHEMA = CHILD.BSCHEMA
AND PARENT.NAME = CHILD.BNAME
AND PARENT.LEVEL < 50
UNION ALL
SELECT
BTYPE AS ROOTTYPE, BSCHEMA AS ROOTSCHEMA, BNAME AS ROOTNAME,
1 as LEVEL, PROC.PROCSCHEMA as SCHEMA,
PROC.PROCNAME as NAME, 'P' as DTYPE,
BTYPE, BSCHEMA, BNAME
FROM SYSCAT.PACKAGEDEP ROOT, PROC PROC
where PROC.PROCSCHEMA = ROOT.PKGSCHEMA
and PROC.PKGNAME = ROOT.PKGNAME
UNION ALL
SELECT
ROOTTYPE, ROOTSCHEMA, ROOTNAME, PARENT.LEVEL+1,
PROC.PROCSCHEMA as SCHEMA,
PROC.PROCNAME as NAME, 'P' as DTYPE, CHILD.BTYPE,
CHILD.BSCHEMA, CHILD.BNAME
FROM RPL PARENT, SYSCAT.PACKAGEDEP CHILD, PROC PROC
WHERE PARENT.SCHEMA = CHILD.BSCHEMA
AND PARENT.NAME = CHILD.BNAME
and PROC.PROCSCHEMA = CHILD.PKGSCHEMA
AND PROC.PKGNAME = CHILD.PKGNAME
AND PARENT.LEVEL < 50
)
SELECT distinct ROOTTYPE, ROOTSCHEMA, ROOTNAME, LEVEL, RPL.SCHEMA,
CASE WHEN DTYPE = 'F'
THEN (SELECT ROUTINENAME FROM SYSCAT.ROUTINES
WHERE ROUTINESCHEMA = RPL.SCHEMA
AND SPECIFICNAME = RPL.NAME )
ELSE RPL.NAME
END AS NAME,
DTYPE, BTYPE, BSCHEMA,
CASE WHEN BTYPE = 'F'
THEN (SELECT ROUTINENAME FROM SYSCAT.ROUTINES
WHERE ROUTINESCHEMA = BSCHEMA
AND SPECIFICNAME = BNAME )
ELSE BNAME
END AS BNAME
FROM RPL, OBJ
where ROOTSCHEMA = OBJ.SCHEMA
and ROOTNAME = OBJ.NAME
ORDER BY ROOTTYPE, ROOTSCHEMA, ROOTNAME, LEVEL
ROOTTYPE ROOTSCHEMA ROOTNAME LEVEL SCHEMA NAME DTYPE BTYPE BSCHEMA BNAME
-------- ---------- -------- ----- ------- -------- ----- ----- ------- --------
T TEST CUSTOMER 1 TEST F_CUST_1 F T TEST CUSTOMER
T TEST CUSTOMER 1 TEST V_CUST_1 V T TEST CUSTOMER
T TEST CUSTOMER 2 TEST P_CUST_1 P F TEST F_CUST_1
T TEST CUSTOMER 2 TEST P_CUST_2 P V TEST V_CUST_1
T TEST CUSTOMER 2 TEST V_CUST_2 V V TEST V_CUST_1
T TEST CUSTOMER 3 TEST F_CUST_2 F V TEST V_CUST_2
T TEST CUSTOMER 4 TEST P_CUST_3 P F TEST F_CUST_2
T TEST CUSTOMER 4 TEST P_CUST_4 P F TEST F_CUST_2
8 record(s) selected
T - Table -- 表
F - UDF -- 用户自定义函数
V - View -- 视图
P - Stored Procedure -- 存储过程
更多精彩
赞助商链接