当前位置:Gxlcms > 数据库问题 > PL/SQL数据库开发那点事

PL/SQL数据库开发那点事

时间:2021-07-01 10:21:17 帮助过:4人阅读

 

例如:要想查看当前用户所拥有的存储过程和存储函数,执行下面的SELECT语句:

SELECT object_name, object_type, authid FROM user_procedures;

数据字典user_objects用来存放当前用户所拥有的所有类型的数据库对象,包括表、视图、触发器、序列、存储过程、存储函数以及程序包等。

下面是有关数据字典ALL_OBJECTS的介绍:

ALL_OBJECTS describes all objects accessible to the current user.

Related Views

  • DBA_OBJECTS describes all objects in the database.

  • USER_OBJECTS describes all objects owned by the current user. This view does not display the OWNER column.

ColumnDatatypeNULLDescription

OWNER

VARCHAR2(128)

NOT NULL

Owner of the object

OBJECT_NAME

VARCHAR2(128)

NOT NULL

Name of the object

SUBOBJECT_NAME

VARCHAR2(128)

 

Name of the subobject(子对象) (for example, partition)

OBJECT_ID

NUMBER

NOT NULL

Dictionary object number of the object

DATA_OBJECT_ID

NUMBER

 

Dictionary object number of the segment that contains the object.

Note: OBJECT_ID and DATA_OBJECT_ID display data dictionary metadata(数据字典元数据). Do not confuse(迷惑) these numbers with the unique 16-byte object identifier (object ID) that Oracle Database assigns(分配) to row objects in object tables in the system.

OBJECT_TYPE

VARCHAR2(23)

 

Type of the object (such as TABLEINDEX)

CREATED

DATE

NOT NULL

Timestamp for the creation of the object

LAST_DDL_TIME

DATE

NOT NULL

Timestamp for the last modification of the object and dependent objects(依赖对象) resulting from a DDL statement (including grants and revokes)

TIMESTAMP

VARCHAR2(19)

 

Timestamp for the specification of the object (对象的时间戳格式)(character data)

STATUS

VARCHAR2(7)

 

Status of the object:

  • VALID

  • INVALID

  • N/A


如果要了解当前用户所拥有的数据库对象类型,可以执行下面的SELECT语句:
SELECT DISTINCT object_type FROM user_objects;

程序包的头部和包体的类型分别为PACKAGE和PACKAGE BODY 。
如果要查看某个数据库对象的详细信息,同样可以执行相应的SELECT语句。

例如,以下SELECT语句用来查看对象“total_income”的详细信息:

SELECT object_name, object_type, status, created FROM user_objects WHERE object_name=‘TOTAL_INCOME‘;

数据字典user_source用来存放存储过程、存储函数和程序包的源代码。

当然,这个视图的目的只是为了查看源代码, PL/SQL程序的执行并不是从这里开始的,因为程序在创建时已经经过了编译,在数据库中以二进制形式存储。

因此,试图通过修改这个数据字典而达到修改存储程序的功能是行不通的。
Oracle在创建PL/SQL程序时,将按照用户在编写时的自然格式,以行的形式存储程序代码,并记录每行的行号,所有代码行合起来就是该程序的源代码。

下面是有关数据字典ALL_SOURCE的介绍:

ALL_SOURCE describes the text source of the stored objects accessible to the current user.

Related Views

  • DBA_SOURCE describes the text source of all stored objects in the database.

  • USER_SOURCE describes the text source of the stored objects owned by the current user. This view does not display the OWNER column.

ColumnDatatypeNULLDescription
OWNER VARCHAR2(30) NOT NULL Owner of the object
NAME VARCHAR2(30) NOT NULL Name of the object
TYPE VARCHAR2(12)   Type of object: FUNCTIONJAVA SOURCEPACKAGEPACKAGE BODYPROCEDURETRIGGER,TYPETYPE BODY
LINE NUMBER NOT NULL Line number of this line of source
TEXT VARCHAR2(4000)   Text source of the stored object

例如,要查看函数tax_per_depart的源代码,可以执行下列SELECT语句:
 SELECT line, text FROM user_source WHERE name=‘TOTAL_INCOME‘;

如果在创建存储过程、存储函数或者程序包时发生了语法错误, SQL*Plus将把错误信息在屏幕上显示,同时Oraclet把错误信息记录在数据字典中
数据字典user_errors就是用来存放当前用户在创建存储程序时发生的错误的。

下面是有关数据字典ALL_ERRORS的介绍:

ALL_ERRORS describes the current errors on the stored objects accessible to the current user.

Related Views

  • DBA_ERRORS describes the current errors on all stored objects in the database.

  • USER_ERRORS describes the current errors on the stored objects owned by the current user. This view does not display the OWNER column.

 

ColumnDatatypeNULLDescription
OWNER VARCHAR2(30) NOT NULL Owner of the object
NAME VARCHAR2(30) NOT NULL Name of the object
TYPE VARCHAR2(12)   Type of the object:
  • VIEW

  • PROCEDURE

  • FUNCTION

  • PACKAGE

  • PACKAGE BODY

  • TRIGGER

  • TYPE

  • TYPE BODY

  • LIBRARY(库)

  • JAVA SOURCE

  • JAVA CLASS

  • DIMENSION(维度)

SEQUENCE NUMBER NOT NULL Sequence number (for ordering purposes)
LINE NUMBER NOT NULL Line number at which the error occurred
POSITION NUMBER NOT NULL Position in the line at which the error occurred
TEXT VARCHAR2(4000) NOT NULL Text of the error
ATTRIBUTE VARCHAR2(9)   Indicates whether the error is an error (ERROR) or a warning (WARNING)
MESSAGE_NUMBER NUMBER   Numeric error number (without any prefix)(数字错误号,没有任何前缀)

例如,在创建存储函数total_income时,错把SELECT
语句中的“WHERE deptno=d_no”写成了“ WHERE deptno=ddn。”,于是发生了错误:

CREATE OR REPLACE PROCEDURE total_income(d_no IN integer:=0)
AUTHID DEFINER
AS
total number;
BEGIN
if d_no=0 then --表示所有部门
SELECT sum(sal+nvl(comm, 0)) INTO total FROM emp;
else --仅表示指定的部门
SELECT sum(sal+nvl(comm, 0)) INTO total
FROM emp
WHERE deptno=ddn;
END if;
dbms_output.put_line (‘总收入:‘|| total);
END;

Warning: Procedure created with compilation errors

为了确定发生的所有错误的位置,执行下列查询语句:

SELECT sequence, line, position FROM user_errors;

为了查看第一个错误的详细信息,需要检索TEXT列的数据:

根据这些错误信息很快便可以确定错误的原因,从而进行纠正。

在很多情况下,发生的多个错误是由同一个原因引起的,只要修改了出现错误的程序代码,多个错误可能一起消失。
这需要用户在编写程序的过程中不断积累经验。
SQL*Plus还提供了一种查看错误信息的简便方法,用show errors命令可以查看当前发生的错误,而不需要了解数据字典的详细结构。

这个命令的用法为:

SHOW ERRORS

或者在查看错误信息时指定发生错误的对象的类型和名称,如:

SHOW ERRORS FUNCTION total_income 

在这种情况下,命令show errors的格式为:

SHOW ERRORS 对象类型 对象名称

PL/SQL数据库开发那点事

标签:

人气教程排行