PL/SQL相关的数据字典

卧龙城 2015-07-26

有时候,我们在PL/SQL开发过程中会遇到以下问题:
1)我的程序到底依赖于哪些数据库对象?
2)哪个包中调用了一个其他包中的子程序或变量?
3)我的哪个子程序的参数使用了不合适的数据类型?
4)我的所有子程序是否都使用了适当的优化级别?

傻一点的做法是到代码里搜。。。
聪明的人会使用以下数据字典视图:

【USER_ARGUMENTS】:包含某模式中所有过程和函数的参数。
【USER_DEPENDENCIES】:包含你拥有的对象间依赖。这个视图最常被Oracle用来标识失效的数据库对象(当该对象依赖的对象发生改变后)。
【USER_ERRORS】:包含你拥有的所有已存储的对象的编译错误。这个视图可被SQL*Plus命令:SHOW ERRORS读取。
【USER_IDENTIFIERS】:11g引入并由PL/Scope编译工具填充。一旦填充,这个视图将包含所有标识符-程序名称,变量等。
【USER_OBJECT_SIZE】:包含对象的大小。事实上,这个视图展示了原始,解析和编译的代码大小。尽管是被编译器和运行时殷勤使用,你也可以用来找到环境中的大程序。
【USER_OBJECTS】:包含一个模式下所有对象信息。可查看对象是否valid,找出所有包含EMP名称的包等。
【USER_PLSQL_OBJECT_SETTINGS】:Information about the characteristics—such as the optimization level and debug settings—of a PL/SQL object that can be modified through the ALTER and SET DDL commands.
【USER_PROCEDURES】: 包含关于存储程序信息,例如AUTHID设置,程序是定义为DETERMINISTIC等特性。
【USER_SOURCE】:包含所有对象的文本源代码。一个非常便利的视图,因为你可以对源代码进行各种分析。
【USER_STORED_SETTINGS】:PL/SQL编译器标记。使用这个视图查看哪个程序用了原生编译(native compilation)。
【USER_TRIGGERS】 和【USER_TRIGGER_COLS】:The database triggers you own (including the source code and a description of the triggering event) and any columns identified with the triggers, respectively. You can write programs against USER_TRIGGERS to enable or disable triggers for a particular table.

1、数据字典基础
数据字典是由多个实例创建的表和视图组成,用户通常只有对数据字典查询权限。
绝大多数的数据字段有三个版本组成:
1)用户视图,USER_开头,包含当前已登录用户拥有的数据库对象信息。
2)全部视图,ALL_开头,包含当前已登录用户已读取的数据库对象信息。
3)管理员视图,DBA_开头,这类视图包含一个实例中所有数据库对象信息,普通用户通常无权访问。

例如:
SELECT * FROM user_objects; –我拥有的所有数据库对象信息
SELECT * FROM all_objects; –我有权读取的数据库对象信息
SELECT * FROM dba_objects; –管理员可访问的整个数据库的对象信息

2、显示存储对象的信息 USER_OBJECTS
包含列介绍(英文太简单不翻译了):
OBJECT_NAME: Name of the object

OBJECT_TYPE: Type of the object, such as PACKAGE, FUNCTION, or TRIGGER

STATUS: Status of the object—VALID or INVALID

LAST_DDL_TIME: Time stamp indicating the last time this object was changed

来看几个例子:

1)显示我模式下所有表:

SELECT object_name 
FROM user_objects 
WHERE object_type = 'TABLE' 
ORDER BY object_name;

2)显示所有失效的对象名:

SELECT object_type, object_name 
FROM user_objects 
WHERE status = 'INVALID' 
ORDER BY object_type, object_name;

3)显示所有今天修改的对象:

SELECT object_type, object_name, 
last_ddl_time 
FROM user_objects 
WHERE last_ddl_time >= TRUNC (SYSDATE) 
ORDER BY object_type, object_name

3、搜索和展现源代码 USER_SOURCE
列介绍:
NAME: Name of the object

TYPE: Type of the object (ranging from PL/SQL program units to Java source and trigger source)

LINE: Number of the line of the source code

TEXT: Text of the source code

例如: 我需要改变包SALES_MGR中CALC_TOTALS过程的参数列表。我想找到哪些地方对该过程进行了调用。

SELECT name, line, text 
FROM user_source 
WHERE UPPER (text) 
LIKE '%SALES_MGR.CALC_TOTALS%' 
ORDER BY name, line

当然,这个查询可能连注释也查出来,还有就是不符合LIKE格式的字符串将无法检索出来,例如:
SALES_MGR.
CALC_TOTALS

那么假设,我们的代码都是比较标准的,这个查询还是做了一个不错的工作。
另外,对于11g而言,你可以使用PL/Scope特性。

4、存储代码的编译设置 USER_PLSQL_OBJECT_SETTINGS
PLSQL_OPTIMIZE_LEVEL: 编译对象的优化级别

PLSQL_CODE_TYPE: 对象的编译模式

PLSQL_DEBUG: Whether or not the object was compiled for debugging 对象是否为调试而编译

PLSQL_WARNINGS: 编译对象的编译警告设置

NLS_LENGTH_SEMANTICS: NLS length semantics that were used to compile the object 编译对象的语义长度设置

找出所有没有采用有效编译时优化的程序单元:

SELECT name
FROM user_plsql_object_settings
WHERE plsql_optimize_level < 2

0级表示未采取任何优化。1表示最低限度的优化。2者都不应该存在于生产环境。

找出那些禁用了编译时警告的程序。

SELECT name, plsql_warnings 
FROM user_plsql_object_settings 
WHERE plsql_warnings LIKE '%DISABLE%';

5、关于过程和函数的详细信息 USER_PROCEDURES
AUTHID: Shows whether a procedure or a function is defined as an invoker rights (CURRENT_USER) or definer rights (DEFINER) program unit 调用者权限或是定义者权限

DETERMINISTIC: Set to YES if the function is defined to be deterministic, which theoretically means that the value returned by the function is determined completely by the function’s argument values 是否确定性

PIPELINED: Set to YES if the function is defined as a pipelined function, which means that it can be executed in parallel as part of a parallel query 是否管道函数

OVERLOAD: Set to a positive number if this subprogram is overloaded, which means that there are at least two subprograms with this name in the same package 是否重载

找出所有运行在调用者权限下的过程和函数

SELECT object_name 
, procedure_name 
FROM user_procedures 
WHERE authid = 'CURRENT_USER' 
ORDER BY object_name, procedure_name

显示所有声明为确定性的函数:

SELECT object_name 
, procedure_name 
FROM user_procedures 
WHERE deterministic = 'YES' 
ORDER BY object_name, procedure_name

6、分析和修改触发器状态 USER_TRIGGERS
TRIGGER_NAME: The name of the trigger

TRIGGER_TYPE: A string that shows if this is a BEFORE or AFTER trigger and whether it is a row- or statement-level trigger (in a trigger that is fired before an INSERT statement, for example, the value of this column is BEFORE STATEMENT)

TRIGGERING_EVENT: The type of SQL operation—such as INSERT, INSERT OR UPDATE, DELETE OR UPDATE—that will cause the trigger to fire

TABLE_NAME: The name of the table on which the trigger is defined

STATUS: The status of the trigger—ENABLED or DISABLED

WHEN_CLAUSE: An optional clause you can use to avoid unnecessary execution of the trigger body

TRIGGER_BODY: The code executed when the trigger fires

找出所有已禁用的触发器:

SELECT * 
FROM user_triggers 
WHERE status = 'DISABLED'

找出所有定义在EMPLOYEES表上的行级触发器:

SELECT * 
FROM user_triggers 
WHERE table_name = 'EMPLOYEES' 
AND trigger_type LIKE '%EACH ROW'

Find all triggers that fire when an UPDATE operation is performed:
找出所有包含update操作触发的触发器

SELECT * 
FROM user_triggers 
WHERE triggering_event LIKE '%UPDATE%'

7、对象依赖分析 USER_DEPENDENCIES
NAME: Name of the object

TYPE: Type of the object

REFERENCED_OWNER: Owner of the referenced object 被引用对象的所有者

REFERENCED_NAME: Name of the referenced object 被引用对象的名称

REFERENCED_TYPE: Type of the referenced object 被引用对象的类型

找出所有依赖于EMPLOYEES表的对象:

SELECT type, name 
FROM user_dependencies 
WHERE referenced_name = 'EMPLOYEES' 
ORDER BY type, name

找出当前模式下ORDER_MGR包依赖的所有对象

SELECT referenced_type 
, referenced_name 
FROM user_dependencies 
WHERE name = 'ORDER_MGR' 
AND referenced_owner = USER 
ORDER BY referenced_type, 
referenced_name

8、分析参数信息 USER_ARGUMENTS
OBJECT_NAME: The name of the procedure or function

PACKAGE_NAME: The name of the package in which the procedure or function is defined

ARGUMENT_NAME: The name of the argument

POSITION: The position of the argument in the parameter list (if 0, this is the RETURN clause of a function)

IN_OUT: The mode of the argument—IN, OUT, or IN OUT

DATA_TYPE: The datatype of the argument

DATA_LEVEL: The nesting depth of the argument for composite types (for example, if one of your arguments’ datatypes is a record, USER_ARGUMENTS will have a row for this argument with a DATA_LEVEL of 0 and then a row for each field in the record with a DATA_LEVEL of 1)

1)找出所有包含LONG参数的程序

SELECT object_name 
, package_name 
, argument_name 
FROM user_arguments 
WHERE data_type = 'LONG' 
;

2)找出所有带有OUT或IN OUT参数的函数。这个地方要注意一下:有经验的编程专家都会告诉我们不要在函数中仅使用IN 参数,
原因是带有OUT 和 IN OUT参数函数不能在SQL中被调用,并且不能用在函数索引中。如果你需要函数返回多块信息,那么请使用
一个存储过程或返回一个RECORD类型。下面的例子找出了违反这个条件的函数:

SELECT ua.object_name, 
2 ua.package_name, 
3 ua.argument_name, 
4 ua.in_out 
5 FROM (SELECT * 
6 FROM user_arguments 
7 WHERE position = 0) funcs, 
8 user_arguments ua 
9 WHERE ua.in_out IN ('OUT', 'IN OUT') 
10 AND ua.position > 0 
11 AND ua.data_level = 0 
12 AND funcs.object_name = ua.object_name 
13 AND funcs.package_name = ua.package_name 
14 AND ( funcs.overload = ua.overload 
15 OR (funcs.overload IS NULL 
16 AND ua.overload IS NULL));

9 总结:这儿有座金矿
This article merely scratches the surface of the application information that can be mined from the data dictionary views in Oracle Database. PL/SQL editors such as Oracle SQL Developer provide user interfaces to many of these views, making it easier to browse their contents.

本文只是拂去了可以从Oracle数据字典视图中挖掘出的应用信息的一层表面。PL/SQL编辑器例如Oracle SQL Developer对很多 视图提供了用户接口,从而更容易的浏览它们的内容。

相关推荐