Professional Documents
Culture Documents
Package Descriptions
Each of the supplied packages is described in the following sections.
The procedures and functions are described following a general
introduction to the package itself. Some of the packages, or the procedures
within the packages, are available in certain PLiSQL releases only. This
is also indicated.
DBMS ALERT
The DBMS_ALERT package is used to send messages between sessions
connected to the same database. Alerts are synchronous, meaning that they
are sent when the transaction commits. lf the transaction rolls back, the
alert is not sent. For more information on alerts, see Chapter 16.
Appendix B: Guide to Supplied packages
BB9
PackageName Description
DBMS-ALERTI Synchronous inter-session communication
DBMS-APPL|CAT|ON lNFo Allows registering of an application for
tracing purposes
DBMS-AQ & DBMS-AQADM Management of the oracre8 Advanced
Queuing option
DBMS_DEFER, Allows you to build and administer
DBMS_DEFER_SYS & deferred remote procedure calls
DBMS_DEFER-QUERY2
DBMS_DDL pLlSeL equivalents for some DDL
commands
DBMS_DESCRIBE Describes stored subprograms
DBMSJOBT Allows scheduling of pLlSeL procedures
DBMS_LOB Manipulation of Oracle8 LOBs
DBMS_LOCK User-defined locks
DBMS-ourPUTl provides screen output in sel*plus or
Server Manager
DBMS-PIPE' Asynchronous inter-session
communication
DBMS_REFRESH & Allows you to manage snapshots
^
DBMS SNAPSHOT2
DBMS_REPCAT, Management of Oracle,s symmetric
DBMS_REPCAT_AUTH & replication facility
DBMS-REPCAT-ADMIN2
DBMS-RowlD2 Allows you to obtain information from a
ROWID, and to convert between aracleT
and Oracle8 ROWIDs
24 complete
discussion of this package is beyond the scope of this book; only a brief
description is included in this Appendix.
890 OracleS PUSQL programming
24 complete
discussion of this package is beyond the scope of this book; only a brief
description is included in this Appendix.'
DBMS_APPLICATION INFO
SET MODULE
This procedure is used to set the value of module for the current session,
and can also be used to set the action. lt is defined with
Appendix B: Guide to Supplied Packages 89 I
READ MODULE
This procedure is used to read the values of module and action for the
current session. This information can also be determined by querying
v$session, but by default only users with the DBA privilege can directly
read the table. READ MODULE is defined with
where module_name is the last value set for module using SET_MODULE,
and action_name is the last value set for action using SET_MODULE or
SET ACTION.
SET ACTION
SET_ACTION is used to just set the value of the action field for the current
session. lt should be used before entering a section of your program, and is
defined with
READ_CLIENT INFO
READ-CL|ENT-|NFo will rerurn the varue last set with sET cltENT
tNFo.
It is defined with
DBMS DDL
The DBMS_DDL package provides pLlSeL equivalents of some useful
DDL
commands which cannot.be used directly in pL/SeL. Although the
?PM:_I9L package can be used to execure these commandi as well,
DBMS_DDL provides a simpler syntax.
ALTER_COMPILE
This procedure is equivalent to the sel commands ALTER pRocEDURE
COMPILE, ALTER PACKACE COi\4PILE, ALTER PACKAGE BODY
COMPILE, and ALTER FUNCTTON COMptLE. The syntax is
Appendix B: Guide to Supplied Packages 893
ANALYZE_OBJECT
This procedure is equivalent to the SQL commands ANALYZE TABLE,
ANALYZE CLUSTER, or ANALYZE INDEX. The syntax is
type VARCHAR2,
schema VARCHAR2,
name VARCHAR2,
method VARCHAR2,
estimate rows NUMBER DEFAULT NULL,
esti mate4ercent NUMBER DEFAU LT NU LL);
DBMS_DESCRIBE
DESCRIBE_PROCEDURE
PROCEDURE DESCRI BE_PROCEDURE (
object_name I N VARCHAR2,
reservedl lN VARCHAR2,
Appendix B: Guide to Supplied Packages
895
reserved2lN VARCHAR2,
overload OUT numberlable,
positi on OUT nu mberJable,
levelOUT numberlable,
argu ment_name OUT varchar2_table,
datatype OUT numberlable,
defau lt_va lue OUT number1able,
i n_out OUT numberlable,
DBMSJOB
The DBMSJOB package is used to schedule PL/SQL jobs to run in
the background. Jobs are scheduled to run at certain times by
background processes. A job is just a stored procedure. lf a job fails,
PL/SQL will try up to 16 times to run the jo.b again until it succeeds. For
more information, see Chapter 18"
Appendix B: Guide to Supplied packages gg|
DBMS LOB
DBMS_LOB is used to manage the four types of Oracle8 large
objects - CLOBs, BLOBs, NCLOBs, and BFlLEs. There are alio
oracle8 oCl functions which are equivalent to the functionality in the
DBMS_LOB package. For more information, see Chapter 21.
DBMS_LOCK
The DBMS-LOCK package is used to create your own user-defined locks.
These locks are managed the same way as other oracle locks. This means
that they can be viewed in the fixed views in the data dictionary. User
locks are prefixed with 'UL' so they do not conflict with oracle locks. For
more information on the DBMS_LOCK package, see the oracle server
Application Developer's Guide and the Oracle ServEr Reference.
898 OracleS PUSQL Programming
ALLOCATE_UNIQUE
This procedure will generate a unique lock lD from a lock name. The
syntax rs
lockname lN VARCHAR2,
lockh and e OUT VARCHAR2,
I
CAUTION
Lock names beginning with ORA$ are
reserved for use by Oracle. The lock name
has a maximum length of 128 bytes and is
case sensitive.
ldentifier Meaning
1 Null mode
2 Row Share mode (ULRS)
3 Row Share Exclusive mode (ULRX)
4 Share mode (ULS)
5 Share Row Exclusive mode (ULRSX)
6 Exclusive mode (ULX)
REQUEST
FUNCTION REQUEST(,d lN INTEGER,
lockmode lN INTECER DEFAULT X MODE,
timeout lN INTECER DEFAULT MAXWA|T,
release on commit lN BOOLEAN DEFAULT FALSE)
RETURN INTECER;
FUNCTION REQUEST(/ockhandle tN VARCHAR2,
lockmode lN INTECER DEFAULT X MODE,
timeout lN INTECER DEFAULT MAX\ryAtT,
release_on_commit lN BOOLEAN DEFAULT FALSE)
RETURN INTECER;
CONVERT
FUNCTION CONVERT('d IN INTEGER,
lockmode lN INTECER t
timeout tN NUMBER DEFAULT MAXWATT)
RETURN INTEGER;
FU NCTTON CONVERT( lockhandle tN VARCHAR2,
lockmode tN tNTECER,
timeout tN NUMBER DEFAULT MAXWATT)
RETURN INTECER;
RELEASE
FUNCTION RELEASE('d IN INTECER) RETURN INTECER;
FUNCTION RELEASE(/ockhandle lN VARCHAR2) RETURN INTECER;
SLEEP
PROCEDURE SLEEF(seconds lN NUMBER);
SLEEP suspends the current session for the specified number of
seconds. The maximum resolution is hundredths of a second, so seconds
can be fractional.
DBMS_OUTPUT
The DBMS-OUTPUT package provides limited output capability to
PVSQL, when used in conjunction with SQL*PIus or Server Manager. lt is
useful for debugging and testing your PL/sQL code' For more information'
see Chapter 14.
902 OracleS PUSQL Programming
DBMS PIPE
The DBMS_PIPE package is similar*to DBMS-ALERT, in that it allows
communication between different sessions connected to the same database.
Messages sent over pipes, however, are asynchronous. Once a message is
sent, it will go through even if the transaction that sent it rolls back. For
more information, see Chapter 16.
DBMS ROWID
The DBMS-ROWID package is used to convert between the OracleZ and
Oracle8 ROWID formats. For more information, see the Oracle] Server
SQL Reference.
DBMS SESSION
The ALTER SESSION command is DDL, and thus is not allowed directly in
PLISQL. The DBMS-SESSION package provides an interface to some of the
options available with ALTER SESSION, callable from PL/SQL blocks. The
DBMS-SQL package can be used as an alternative to DBMS-SESSION,
since it allows execution of arbitrary statements, inclLrding ALTER SESSION.
Appendix B: Guide to Supplied packages
903
SET_ROLE
PROCEDURE SET_ROLE (role cmd VARCHAR2);
sET_SQL_TRACE
PROCE D U RE SET_SeL_TRAC E(sgl_frace B OOL EAN ) ;
SET NLS
PROCEDURE SET_NLS( param VARCHAR2, valueVARCHAR2);
CLOSE_DATABASE LINK
PRocE D u R Ec L.'E-DATA BAS E-L r N K( dblink v,q RcHn nz t ;
ThiS PTOCCdUTC iS CqU|VAICNI tO ALTER SESSION CLOSE IfATABASE
LINK dblink' rt closes an impricit connection
to a remote database.
SET_LABEL
PROCEDURE SET LABEL(/b/ VARCHAR2);
SET_MLS_LABEL_FORMAT
PRocEDURE LABEL FoRMAT(fmr VARCHAR2);
'ET-MLS
Also valid in Trusted oracre, sET_MLS LABEL_FORMAT
is equivarent
to ALTER sESSroN sET MLS_LAsrL
_ronv AT = imt- tLchrnges the deiault
label format for the current ,ession.
RESET_PACKAGE
PROCEDURE RESET PACKACE;
UN|QUE_SESS|ON tD
FUNCTTON UNTQUE_SESS|ON lD RETURN VARCHAR2;
IS ROLE ENABLED
FUNCTION lS ROLE ENABLED(rolenameVARCHAR2)
RETURN BOOLEAN;
This function returns TRUE if rolename is enabled for this session and
FALSE otherwise. lf lS ROLE ENABLED is cailed from a stored subprogram
or trigger, it will always return FALSE since all roles are disabled there.
DBMS_SQL
eE DBMS_SQL irnplements dynamic PLISQL. Using this package, your
program can construct SQL statements and PL/SQL blocks at run
time and execute them. DBMS_SQL can also be used to execute DDL
statements from PL/SQL, which are not perrnitted otherwise. For more
information, see Chapter 15.
DBMS TRANSACTION
The DBMS TRANSACTION package provides procedures for
transaction management. Many of the commands available here are
also available in their SQL equivalents directly in PL/SQL. They are
i ncl uded for completeness.
COMMIT Commands
PROCEDURE COMMIT;
PROCEDU RE COMMIT_COMMENT(cmnt VARCHAR2);
PROCEDURE COMMIT FORCE(
xidVARCHAR2,
scn VARCHAR2 DEFAULT NULL);
B EG I N_D I SC RETE_TRANSACTION
PURGE_MIXED
PROCE D U RE PU RCE_M tXED(xid VARCHAR2);
LOCAL_TRANSACTION ID
FUNCTION LOCAL TRANSACTION-ID(
create_transact i o n BOOLEAN DEFAU LT FALSE)
RETURN VARCHAR];
STEP ID
FUNCTION step_id RETURN NUMBER;
DBMS_UTILITY
The DBMS-urLrry package provides additionar
functionarity for
managing procedures, reporting errors, and other information.
COMPILE-SCHEMA
PROCEDURE COMptLE SCHEMA(schema VARCHAR2);
ANALYZE_SCHEMA
PROCEDURE ANALYZE SCHEMA(
schema VARCHAR2,
method VARCHAR2,
esti mate_rows N UMBER DEFAULT N U LL,
esti mate_percenr N UMBER DEFAULT NU LL);
FORMAT_ERROR_STACK
F U NCTI ON FORMAT-E RROR_STACK RETU R N VARC HAR2;
FORMAT_CALL_STACK
FUNCTION FORMAT CALL STACK RETURN VARCHAR2;
IS_PARALLEL_SERVER
FUNCTION IS-PARALLEL-SERVER RETURN BOOLEAN;
GET TIME
FUNCTION CET TIME RETURN NUMBER;
DECLARE
v-SIart NUMBER;
v_End NUMBER;
BEGIN
V STATT := DBMS UT'ILITY.GET -TIME;
/* Do some work here */
v End := DBMS UTILfTY.GET TIME;
/* The work took (v_Start - v_End) * 100 seconds to execute. */
END;
NAME RESOLVE
PROCEDURE NAME RESOLVE(
NAMC IN VARCHAR2,
context lN NUMBER,
schema OUT VARCHAR2,
partl OUr VARCHAR2,
paft2 OUr VARCHAR2,
dblink OUT VARCHAR2,
parrl_type OUT NUMBER,
obj ect_nu mber OUf N UMB ER);
PORT STRING
FUNCTION PORT STRINC RETURN VARCHAR2;
This function returns a string that uniquely identifies both the version of
Oracle and the operating system. The maximum length is dependent on the
operating system.
UTL FILE
The UTL-FILE procedure implements file l/O in PLISQL. Using this
iltd'.Tlil package, PL/SQL programs can read from and write to operating
system files located on the server. The accessible files and directories are
limited by parameters in the INIT.ORA database initialization file, for
security. For more information, see Chapter 18.
914 Oraete8 pUSeL programming
DECLARE
declarative section here
-
BECIN
executable section here
-
EXCEPTION
exception_handling section here
-
END;
coMMrT [woRK];
where the WORK keyword is optional. For more information, see Chapter 4.
Cursors Cursors are used to control the processing for queries that
return more than one row. A cursor is declared using the CURSOR..IS
syntax, then processed with OPEN, FETCH, and CLOSE. Cursor attributes
are used to determine the current status of a cursor, with information about
how many rows the cursor has returned, whether the cursor is open, and
whether the last fetch was successful. For more information, see Chapter 6.
918 OracleS PUSQL programming
Table specifies the table from which the rows should be deleted. An alias
for the table name can also be specified if desired. rhe where clause
determines which rows will be deleted. lf the CURRENT.oF cursor clause
is used, then the last row fetched trom cursorwill be deleted. After a delete
that removes one or more rows, SQL%NOTFOUND is FALSE, SeL%FOUND
is TRUE, and sQl%RowcouNT contains the number of rows deleted. rf
the wherejclause does not match any rows, no rows are deleted and
SQL%NOTFOUND is TRUE, SQL%FOUND is FALSE, and
SQL%ROWCOUNT equals 0. For more information, see Chapter 4.
Functions Functions are named pl/sel blocks that return a value and
can be called with arguments. They can be used as part
oian expression.
legal in prccedural statements, and certain functions
Ih:L1r"
in sQL statements in PL/seL 2.r and higher. Functions
can be used
."n b" stored in the
database with the CREATE tOR REPLAC:I FUNCTION
.ornr.n.J, or they
can be located in the declarative sec:ion of another block.
For more
information, see Chapter Z.
COTO label;
character set. ldentifiers must begin with a letter. For more information,
see Chapter 2.
lF conditionl THEN
sequ ence_of_statements 1 ;
[ELSIF condition2THEN
s eq uence ate m e n t s 2 ;)
-of -st
IELSE
se q u e n c e_of_state m e n ts 3 ;]
END IF;
where tableis a reference to the table into which the rows will be insefted,
and expression-list is a comma-separated list of expressions that make up
the fields of the new row. lf column /ist is specified, then it determines the
columns that will have values. Columns that are not specified will have a
value of NULL. lf column-list is not specified, then expression-list should
correspond to all the columns in table. For more information, see Chapter 4.
where tableis a reference to the desired table, and lock_mode specifies the
mode. Available modes include ROW EXCLUSIVE, ROW SHARE, SHARE
UPDATE, SHARE, SHARE ROW EXCLUSIVE, and EXCLUSIVE. lf NOWAIT
is specified, the statement will return immediately either with the lock
acquired or with TIMEOUT_ON RESOURCE raised if the lock cannot be
acquired. lf NOWAIT is not speciiied, then the LOCK TABLE will wait
until the lock can be acquired. For more information, see the Oracle Server
SQL Reference.
cursofloNOTFOUND I cursor_variable%NOTFOUND
NULL;
called with parameters. The parameters can either accept a value from the
calling environment (lN), return a value to the calling environment (OUT),
or both (lN OUT). For more information, see Chapter Z.
RAISE lexception_namel;
DECLARE
TYPE t_StudentT\@e rS RECORD (
where function name is the function whose purity yoLt are asserting. The
purity levels, defined in the following table, can be specified in any order.
For more information, see Chapter B.
Purity
Level Meaning
RNDS Reads no database state
WNDS Writes no database state
RNPS Reacls no package state
WNPS V\ir"ites no package state
the work done after savepoint is rolled back. For more information, see
Chapter 4.
DECLARE
v_ClassInf o cf asses%ROWTypE ;
The variables in variable list should match the items in select_lisf or the
fields in rhe record should match. The row will be retrieved from the table
identified by table. The rest of the clauses determine which row will be
retrieved. lf the query returns more than one row, use an explicit cursor. lf
the query does not return any rows, the No_DATA_FouND exception is
raised. For more information, see Chapter 4.
SET TRANSACTION
USE ROLLBACK SECMENT segmenf I READ ONLy;
subscript value greater than the limit for avarrayt or non-positive for a
varray or nested table. For more information, see Chapter 12.
see Chapter 4.
Tables PL/SQL tables are syntactically treated much like arrays in other
third-generation languages. To declare a PL/SQL table, first you define a
new table type and then a variable of that type. For example, the following
declarative section defines a table of DATEs:
DECLARE
TYPE t DateTable TS TABLE OF DATE
INDEX BY BINAR.Y-INTEGER;
v_Dates t_DateTable;
DECLARE
v_Fi-rstName students . f irs t_name%TypE ;
v_Currentcredits students . current_credits?TypE;
where variable nameis the name of the new variable or constant, and
type is its type. type can be either a predefined type, such as DATE or
ROWID, or a user-defined type. lf CONSTANT is specified, the value of
the variable cannot be changed; it is a constant. The variable or constant
will be assigned initial_value if it is specified or NULL if no initial value
is specified. For more information, see Chapter 2.
DECLARE
TYPE t_DaTes IS VARRAY(25) OF DATE;
Naming Conventions
Many of the views have three different instantiations. These are known as
user_*, all_*, and dba_*. For example, there are three instantiations of the
information about the source for stored objects. The views that represent
this are user-source, all_source, and dba_source. ln general, the user_*
views contain information about objects owned by the current user, the
all * views contain information about all objects accessible to the current
user (not necessarily owned by them), and the dba * views contain
information about all objects in the database.
SQL and PL/SQL are not tase sensitive. ln order to implement this, all
objects are convefted into uppercase before they are stored. Therefore, you
should use uppercase whcn querying the data dictionary. For example, the
user_objects view has a column object name that contains the name of the
Appendix D: The Data DictionarY 939
object. These names are always stored in uppercase. You could query
user-obiects with:
OBJECT-TYPE STATUS
PACKAGE VAL]D
PACKAGE BODY VALID
Note the use of the UPPER function so that the query will return the desired
rows. (ClassPackage is described in Chapter B')
Permissions
The data dictionary views are owned by sYS. By default, only sYS and
users with the DBA system privilege can see all of the views. Users
without
the DBA privilege can see the user-* and all-* views, in addition to Some
others. They cannot see the dba-* views unless they have been granted
specific SELECT privileges on them.
The data diction"ty-ui"*t should never be updated, even by SYS. They
are updated automaticallyby their r
changes. Oracle also provide fy the
when-a database is uPgraded (such
upgrades from a 7.3.1 database to a 7 abase)
founa in the same directory as catproc'sql'
Dependencies
The all_dependencies, dba_dependencies, and user-dependencies
views
documEnt ihe dependency relationship between stored objects.
940 OracleS PUSQL programming
Category Views
Dependencies all_dependencies, dba_dependencies,
user _dependencies
Collections* all_coll_types, dba_coll_types, user_coll_types
Compile Errors all_errors, dba_errors, user_errors
Directories* all directories, dba_directories, user_directories
Jobs all_jobs, dba_jobs, useljobs
LOBs* all_lobs, dba_lobs, user_lobs
Libraries* all libraries, dba_libraries, user_libraries
Object Method all method_params, dba_method_params,
Parameters* user method_params
object Method Results all method_results, dba_method_results,
(return values)* user method results
Object Methods* all_type_methods, dba_type_methods,
user_type_methods
ObjectReferences* all_refs,dba_refs,user_refs
Object Type Anributes+ all_type_attrs, dba_type_attrs, user_type_attrs
Object Types+ all_types, dba_types, user_types
Schema Objects all_objects, dba_objects, user_objects
Source Code all_source, dba_source/ user_source
Table Columns all tab columns, dba_tab_columns,
user tab columns
Tables all_tables, dba_tables, user__tables, all_catalog,
dba_cata log, user_catalog
Triggers all_triggers, dba_triggers, user_triggers
Trigger Columns all_trigger_cols, dba_trigger_cols,
user_tngger cols
* Available in OracleB
anct higher
Appendix D: The Data Dictionary 94a
Collections
The all_coll_types, dba_coll_types, and user_coll_types views
contain information about the collections accessible to the
user. only collection types which are defined with the CREATE TypE
statement (as opposed to those local to a pLlSeL block) are stored in the
data dictionary.
942 Oracle8 PUSeL programming
Compile Errors
The all-errors, dba-errors, and user_errors views contain the text of
compile errors for stored objects and views. lf there is an entry in one of the
_errors views, then the object is necessarily invalid (indicated in
all_objects, dba_objects, and user_objects).
A typical query of user._errors. might look like this:
Directories
ffi ffi ::;,:!"'-ffi ,*i:? ji:;fnffi ;:t1,*x,$:r;u:L::#j"
of directories (for BFILEs) on the operating system. For more information,
see Chapter 21.
f obs
The alljobs, dbajobs, and user_jobs views contain information
about database jobs.
944 OracleS PUSQL Programming
Libraries
The all-libraries, dba-libraries, and user_libraries views specify the
operating system location for shared libraries, used for external
procedures. For more information, see Chapter 20.
dba_libraries only)
LIBRARY NAME NOT VARCHAR2(3O) Name of the library
NULL
FILE SPEC VARCHAR2(2000) Operating system patch of
the library on the file system
DYNAMIC VARCHAR2(1) Y if the library is dynamic, N
if not
STATUS VARCHAR2(7) Library status-VAltD or
INVALID
LOBs
The all_lobs, dba_lobs, and user_lobs contain information about
LOBs contained in tables accessible by the current user.
945 OracleS pUSeL programming
Object Methods
I he all_type_methods, dba_
The dba_type_methods, and userJype_methods
views contain information about the methods in the objectiypes
accessible to the current user.
Object References
The all_refs, dba-refs, and user refs views contain information
about the columns in the tables accessible to the user which are
REF types.
Schema Objects
The all-obfects, dba_objects, and user_objects views contain information
about all types of schema objects, including tables, stored subprograms,
views, sequences, and indexes. Object types are described in ihe
*_type_methods, *_method_;rarams, *_method_results, *
*_types views. _type_attrs, and
Source Code
The all source, dba_source, and user_source views contain the source
code for stored procedures, functions, packages, and package bodies.
Trigger source code is in the all_triggers, dba_triggers, and userJriggers
views. lf the stored object is wrapped, these views contain the encoded
source rather than clear te>ct.
A typical query of the user_source table might look like this:
SELECT text
FROM user source
WHERE NAME = object_name
ORDER BY LINE.
Tables
The all rables, dba-tables, and user_tables views contain information
about database tables. This information is for the table itself. column
information is stored in all_tab_columns, dba_tab columns, and
user tab columns.
Table Columns
The al l_tab_col umns, dba_tab_columns, and userJab_columns vi ews
contain information about columns in database tables, views, and clusters.
When an object is described, this table is queried.
Appendix D: The Data Dictionary 955
Triggers
The all_triggers, dba_triggers, and userJriggers views
describe the
oarabase triggers accessible to the user. All of the different
components in
the CREATE TRlccER statement are corumns in these
views.
Trigger Columns
The al l-trigger-cols, dba_tri gger_cols, and userJri gger_cols v ews show
i
Views
The all-views, dba-views, and user_views views describe the views in the
database.
dbms_alert_info
The dbms alert info view contains information about sessions that have
registered interest in alerts.
dict_columns
The dict_columns view describes all of the columns in the data dictionary.