Beruflich Dokumente
Kultur Dokumente
•= PL/SQL provides seamless and tight integration with SQL, with natural, efficient, and safe
extensions to the SQL language.
•= An inherent strength that PL/SQL has is the power to evolve the language, which helps to naturally
and easily solve customer problems.
•= Embedded in the Oracle Server, the PL/SQL platform provides excellent support for PL/SQL stored
procedures as well as the ability to build scalable, portable, and highly robust Enterprise
applications on the server using these.
•= Embedded in the Oracle Developer™ and Oracle Developer Server™ products, the PL/SQL platform
provides a consistent application development environment, including the ability to drag and drop
procedures across tiers. PL/SQL procedures may be compiled and executed in the client or
middle tier.
As more and more applications deployed at the Enterprise level use PL/SQL heavily, there is an
increasing need to make the PL/SQL application development platform easier to use.
Oracle8i PL/SQL Release 8.1 includes a number of improvements that ease the development of
applications based on Oracle. Some of the improvements are transparent, allowing application
developers to benefit from these almost immediately, while some of the improvements involve new
language features that make programming applications much simpler. Oracle8i PL/SQL Release 8.1
™
also provides seamless interoperability with Java , executing in the server, and with C procedures,
executing in a separate process.
This paper focuses on the ease-of-use improvements and application development enhancements in
Oracle8i PL/SQL Release 8.1.
For a general overview of all Oracle8i PL/SQL Release 8.1 features, and for a discussion on future
directions, please refer to [1], an Oracle technical white paper on “Oracle8i PL/SQL - New Features
and Future Directions.”
The following table summarizes the new Oracle8i PL/SQL Release 8.1 features that make PL/SQL
much easier to use:
Areas Features
Transparent •= Ability to compile much larger package bodies, by relaxing an internal limit
Ease-of-use that gave rise to the compilation time error “PLS-123: program too large”.
Enhancements •= Improved diagnostics for “ORA-6502: numeric or value error”, with the use of
additional subtext in many cases.
•= Improved ability to log diagnostic messages at runtime much more easily, using
the new autonomous transactions feature.
•= Ability to create procedures that execute with the caller’s privileges (invoker-
rights procedures).
Tools for •= Support for tracing, profiling and performing code-coverage of PL/SQL
Application programs through the use of new “Probe” API extensions. These extensible,
open family of APIs, which included support for debugging in previous
rd
Development releases, may be used by 3 party vendors to build development environments
to debug and monitor PL/SQL programs
Prior to Oracle8i PL/SQL Release 8.1, the compilation of very large package and type bodies and
anonymous blocks resulted in the compilation time error “PLS-123: Program too large”, due to an
internal compiler limit. To work around this limitation, application developers split their packages
and type bodies into smaller units. In Oracle8i PL/SQL Release 8.1, this is no longer necessary,
because the internal compiler limit has now been substantially increased, allowing much larger
package and type bodies to be compiled. While it is not easy to correlate this internal limit with
number of lines of PL/SQL source code, it is estimated that an application developer would now be
able to write up to 6 millions lines of code in a single package or type body, provided other limits
aren’t reached. Note that the limit for other types of program units (schema-level functions and
procedures, package and type specs) has not been increased.
In Oracle8i PL/SQL Release 8.1, the runtime ORA-6502 error message that customers frequently
encounter is now improved to display subtext that state the reason (in most cases) why the error
occurred. This should help customers find problems in their code much more easily. For example:
declare
x varchar2(1);
begin
x := '12';
end;
Instead of seeing the more cryptic “ORA-6502: numeric or value error”, the application developer
will now see the following, much more informative error: “ORA-6502: PL/SQL: numeric or value
error: character string buffer too small”. In the case of:
declare
x number;
begin
x := 'abc';
end;
The application developer will see the following, much more friendly error: “ORA-6502: PL/SQL:
numeric or value error: character to number conversion error ”.
Subtexts for the ORA-6502 exceptions have been added for the most common ones that the PL/SQL
runtime engine throws, such as conversion errors. In future releases, Oracle expects to improve more
of the ORA-6502 messages, by adding more subtexts.
Although this new functionality doesn't implement the full blown parameterized exceptions
functionality that would be nice to have, it is an initial step in that direction, and should aid PL/SQL
developers who find the generic 6502 exception message hard to use.
IMPROVED INTEROPERABILITY
Although PL/SQL procedures and functions stored in an Oracle7™ database or an Oracle Developer
product containing PL/SQL Release 2.3 can make calls to all remote packaged procedures stored in a
remote database, they can call only those schema-level procedures that do not contain the use of any
new Oracle8™ features. In Oracle8i, this limitation no longer applies. This means that a procedure
stored in an Oracle7 or Oracle8 database, or an Oracle Developer product that contains PL/SQL
Release 2.3 or Release 8.0 may now call all remote procedures. This is true, regardless of whether the
remote procedures are packaged procedures or schema-level procedures and whether these remote
procedures contain the use of any new Oracle8i PL/SQL Release 8.1 features.
The purity of a stored procedure refers to the side-effects of that procedure on database tables or
packaged variables. Various side-effects are disallowed when a function is called from a SQL query or
DML statement. This is discussed in [6] “Oracle8 Application Developer’s Guide, Release 8.0”.
The mechanism in Oracle7 and Oracle8 leveraged the PL/SQL compiler, to enforce restrictions
during the compilation of a stored procedure, or the compilation of a SQL statement. In Oracle8i
PL/SQL Release 8.1, the compilation time restrictions have been relaxed, and instead a smaller set of
restrictions are enforced during execution. This change was made with two goals in mind:
•= To provide uniform support for stored procedures written in PL/SQL, Java, and C.
In addition, many restrictions have been safely removed in order to allow programmers the most
flexibility possible.
•= A function called from a query (SELECT statement) or a parallelized DML statement does not
execute any DML or in any other way modify the database.
•= A function called from a query or DML statement does not end the current transaction, create or
rollback to a savepoint, or ALTER the system or session.
•= A function called from a DML statement does not read or modify the particular table that that DML
statement modifies.
Pragma RESTRICT_REFERENCES may still be used, if desired, to request that the PL/SQL
compiler identify statements or calls that have side-effects beyond those expected. A new keyword,
TRUST, has been added to the pragma RESTRICT_REFERENCES syntax. This allows the easy
calling of a subprogram that does not have the pragma from one that does by creating a wrapper
subprogram that uses the pragma with this new keyword that then calls the subprogram that does
not have the pragma.
•= The DETERMINISTIC keyword is a hint to indicate that the function returns results in a
deterministic manner and, therefore, need not be called redundantly. This functionality is new in
Oracle8i PL/SQL Release 8.1, and is used in the context of some new features, like
functional indexes.
•= The PARALLEL_ENABLE keyword is a hint to indicate that a function may be called from within
a parallel_query or parallel DML statement. In releases prior to Oracle8i PL/SQL Release 8.1, this
property was indicated in a somewhat non-obvious manner, via the WNPS and RNPS options of
the pragma RESTRICT_REFERENCES syntax.
Pragma RESTRICT_REFERENCES may still be used to request that the PL/SQL compiler identify
statements or calls that have side-effects beyond those expected.
Dynamic SQL refers to the ability to dynamically build and submit SQL statements for execution,
while in the midst of an execution of a PL/SQL procedure. Since the content of these statements can,
and probably will change with every execution, they are referred to as dynamic SQL statements.
Dynamic SQL statements may include data manipulation language (DML) statements (i.e., INSERT,
UPDATE, DELETE, SELECT), PL/SQL anonymous blocks, data definition language (DDL)
statements, transaction control statements, session control statements (with some restrictions), etc.
Dynamic SQL is very useful for applications that allow users to choose query search criteria or
optimizer hints at run time. Such uses are often required in database query tools, in interactive
database applications that present the user with information specific to their needs, and in
application code that is reusable and, therefore, independent of the actual SQL statement
being executed.
Prior to Oracle8i PL/SQL Release 8.1, the only way to program dynamic SQL statements inside
PL/SQL procedures involved the use of the programmatic interface provided through the
DBMS_SQL package. While this interface addresses the basic need for the dynamic SQL
functionality, it tends to be somewhat cumbersome to use, and generally results in code clutter and
code explosion.
Oracle8i PL/SQL Release 8.1 now provides the dynamic SQL capability directly through the PL/SQL
language, with new syntax and extensions of existing syntax. The new native dynamic SQL
functionality provides the following benefits over DBMS_SQL:
•= The new syntax is highly intuitive, much easier to use, and highly conducive to quick coding.
•= Programs written using the new functionality are a lot more compact, readable, and maintainable.
•= The performance of programs that use the new native dynamic SQL functionality is substantially
better than those that use the DBMS_SQL interface.
The PL/SQL language has been extended to support dynamic SQL in the following ways:
•= A new EXECUTE IMMEDIATE statement has been added, to allow the application developer to
prepare, execute, and deallocate a dynamic SQL statement. For example:
OPEN ref_cursor_var
FOR 'select * from ' || table_name || ' where sal > :s '
USING salary;
•= The existing FETCH and CLOSE statements have been extended to support dynamic cursors
without any syntax changes.
The example below demonstrates the benefits of native dynamic SQL over the DBMS_SQL package:
IS IS
-- dynamic SQL
dbms_sql.parse(cur_hdl, stmt_str,
dbms_sql.native);
-- supply binds
dbms_sql.bind_variable(cur_hdl,
':deptno', deptnumber);
dbms_sql.bind_variable(cur_hdl,
':dname', deptname);
dbms_sql.bind_variable(cur_hdl,
':loc', location);
-- execute cursor
rows_processed :=
dbms_sql.execute(cur_hdl);
-- close cursor
dbms_sql.close_cursor(cur_hdl);
END insert_into_table;
The new dynamic SQL capability also provides significant performance improvements over
DBMS_SQL. Internal benchmarks using Oracle8i PL/SQL Release 8.1.3 demonstrated a 1.5 to 3.2
times improvement in performance. For more information about the performance benefits of this
For more details on the new native dynamic SQL capability, please refer to [2], an Oracle technical
white paper on “Native Dynamic SQL in PL/SQL”.
In many applications, it is often required to perform various logging operations (e.g., error logging,
audit logging) without being affected by the current transaction context. In releases prior to
Oracle8i PL/SQL Release 8.1, this functionality could be implemented only using DBMS_PIPE,
which required the calling procedure to communicate with a daemon process that performed the
logging in a separate session and transaction. While DBMS_PIPE is very powerful and flexible,
programming with DBMS_PIPE is often tedious and results in code that is cumbersome, hard to
read and maintain, and inefficient.
Oracle8i introduces autonomous transactions, to much more effectively solve such problems. An
autonomous transaction is an independent transaction started within the context of another
transaction, the parent transaction. It shares no locks, resources, or commit-dependencies with the
parent transaction. Autonomous transactions provide all the functionality of regular transactions.
They also allow parallel queries, distributed processing, and all transaction control statements.
More importantly, autonomous transaction blocks help the application developer build modular
reusable software components that perform specialized transactional operations. This may be done
by writing stored procedures to independently start and finish autonomous transactions independent.
A calling application need not know about a procedure’s autonomous transactional operations, and
the procedure need not know about the calling application’s transaction context. This makes
autonomous transactions much less error-prone and much easier to use than regular transactions.
BEGIN
END debug_insert1;
pragma AUTONOMOUS_TRANSACTION;
BEGIN
commit;
END debug_insert1;
BEGIN
ROLLBACK;
/*
*/
END;
Prior to Oracle8i, the only privilege and name-resolution context model that was supported was the
definer-rights model. In this model, a PL/SQL stored procedure may execute only with the privileges
of the user that created the procedure, and in the name-resolution context of that user’s schema.
Before allowing the procedure to execute any operation on a database object, Oracle verifies that the
procedure’s owner has the required privileges. In addition, stored procedures are bound to their
owner’s schema; i.e., unqualified object names are resolved in the procedure owner’s schema.
Definer-rights procedures help the application developer encapsulate access to database objects that
may not be public.
Oracle8i introduces a new complementary privilege and context model with invoker-rights procedures.
An invoker-rights procedure inherits the privileges and name-resolution context of the user from the
calling procedure. Although Oracle supplies several DBMS_* packages that behave like invoker-
rights packages (for instance, DBMS_SQL accepts a SQL string from a user and parses and executes it
on behalf of the user, despite being owned by SYS), the mechanism was internal and restricted to a
few packages in prior releases. Oracle8i formalizes and exposes these mechanisms to all users.
Invoker-rights stored procedures provide the ability to write and install a single copy of code that
multiple users can invoke to manipulate objects in different schemas, depending upon the calling
context. This can be extremely useful if an application must execute SQL on behalf of the user
calling it.
An invoker-rights procedure may be specified, using the new AUTHID clause. For example:
The next few sub-sections walk the reader through a simple example that compares the two models.
Consider a company database that stores sales information for a number of regional company sales
offices. Sales data is stored in table SALES_TAB in each region’s schema, and accessed through
sales, an application package that encapsulates access to the data in SALES_TAB.
úü úü úü
ÿÿ ÿÿ ÿÿ
Figure 1. Sales database for a company with multiple regional sales offices.
Broken lines indicate package invocation. Solid lines indicate table access. A
separate copy of definer-rights package sales is installed in each region’s schema.
The application package sales must have the privileges to access SALES_TAB for each region. One
way to achieve this is to write sales as a definer-rights package, and then install a separate copy in
each region’s schema. In this case, the technique of code replication solves the access-rights problem,
at the cost of reduced maintainability.
Alternatively, an application designer may choose to install a single copy of definer-rights package
sales in a central schema called COMPANY (see figure below). This requires two additional steps:
•= Sales must be extended (to a new version sales+), to take its calling context into account, i.e., each
invocation of sales must identify its region invoking it, and update SALES_TAB in that region. The
calling context may be passed in, to sales+, as an extra argument whose accuracy sales+ must be
able to verify.
ÿúü
ÿÿ ÿÿ
If sales is implemented as an invoker-rights package, each invocation of sales inherits privileges and
name-resolution context from the user calling it (see figure below). In other words, if REGION1
invokes sales, each SQL operation (query, DDL or DML) in sales behaves as though REGION1
executes it. Access to SALES_TAB resolves to the table in REGION1, and is tested against user
REGION1’s rights.
úü
ÿÿ ÿÿ
The application developer must be aware that any invoker-rights program that a user executes will
inherit all the privileges of the user, and can do anything that the user is able to do. Therefore, the
user must only execute such a program if the user trusts that it will not do something malicious.
In releases prior to Oracle8i PL/SQL Release 8.0, queries may be performed only against database
tables. Starting with Oracle8i PL/SQL Release 8.1, Oracle provides support for SQL queries on local
variables of nested table or varray types. In other words, users can now specify a PL/SQL local
variable or parameter of a nested table or varray type, in the FROM clause of a SQL query. In Release
8.0, the syntax for doing this was somewhat clumsy and involved the use of the THE operator, which
Oracle plans to deprecate in a future release. In Release 8.1, a new, much improved syntax is now
available through the use of the TABLE() operator. This feature provides a powerful way of querying
dynamically created in-memory data.
DECLARE
BEGIN
lvnt(3) := 'HUNDRED';
SELECT cast_lvnt.COLUMN_VALUE
SELECT cast_lvnt.COLUMN_VALUE
END;
Note that this feature is currently available only with nested table types and varray types but not
with PL/SQL index-by table types.
USER-DEFINED OPERATORS
In Oracle8i Release 8.1, users may define schema-level operators and bind them to PL/SQL functions.
These operators may subsequently be used in SQL statements wherever a SQL built-in operator is
allowed. Furthermore, one or more user-defined index types may optionally support an operator.
This means that an index of this index type can be used in efficiently evaluating these operators.
This feature is very important for specialized datatype developers. For example, Oracle's ConText™
datatype has implemented CONTAINS as a user-defined operator.
STATIC METHODS
Oracle8i PL/SQL Release 8.1 now supports the ability to define static methods, i.e., those that do not
have SELF as a parameter. Static methods are very useful in applications, since static methods that
logically belong to the object type, but do not operate on the SELF parameter, may now be packaged
in the object type itself.
The Oracle8 Server, Release 8.0 provides the ability to call, from PL/SQL stored procedures, C
external procedures executing in separate processes. Oracle8i PL/SQL, Release 8.1 additionally
provides the ability to call Java stored procedures executing in the server.
•= The application programmer now has the option of providing the CALL-specification, either in a
PL/SQL package (or type) specification, or in a PL/SQL package (or type) body.
•= This new syntax allows an application developer to specify mappings from PL/SQL procedures to
procedures and methods in other languages (such as C and Java), consistently across these other
languages.
•= CALL-Specifications for C and Java procedures may coexist within a single PL/SQL package or type
specification, and may be called from either SQL or PL/SQL.
Here is an example of two CALL-Specifications (one for C and one for Java) in a single
package specification:
LANGUAGE C
LIBRARY some_c_library
NAME "c_call"
LANGUAGE JAVA
NAME 'java_call_class.java_call(oracle.sql.NUMBER)';
END call_spec_pack;
In Oracle8i PL/SQL Release 8.1, it is now much easier to call C programs from PL/SQL procedures:
•= Complete support for Oracle8 objects and collections types: Oracle8 objects and collection types may now
be passed to the external procedure. New objects and collections may be returned from the external
procedure as IN/OUT, OUT, and RETURN arguments.
•= Support for additional mappings: PL/SQL type NUMBER to C type OCINumber; PL/SQL type
DATE to C type OCIDate; PL/SQL types VARCHAR2, LONG and CHAR to OCIString; PL/SQL
types RAW and LONG RAW to C type OCIRaw.
•= Caching of DLLs for improved external procedure performance: Oracle8i external procedures transparently
cache up to ten DLLs in a session. Subsequent calls to a C function in a cached DLL will not cause
the DLL to be reloaded, thereby significantly improving external procedure performance.
•= Support for Pro*C to execute SQL callbacks into the database: Prior to Release 8.1, external procedures
could call back into the database only through OCI. In release 8.1, external procedures now support
Pro*C to callback into the database. Writing Pro*C to execute external procedures SQL callbacks is
substantially easier than writing OCI8 callbacks.
OCIExtProcContext *with_context;
OCINumber *empnumber;
short empnumberind;
exec sql
....
....
Oracle8i provides strong support for Java in all tiers, including Java stored procedures, with a Java
Virtual Machine (VM) executing in the server. Oracle8i PL/SQL and Java interoperate seamlessly
and complement each other in the database. Consistent with Oracle’s theme of “language
transparency”, Oracle addresses PL/SQL and Java interoperability in two ways:
•= Application level interoperability: PL/SQL and SQL can call Java methods by registering call
specifications for the Java class. Java can also call PL/SQL and SQL, either through the JDBC
interface or through the use of SQLJ. In the future, Oracle plans to allow PL/SQL and Java stored
procedures in the server to be debugged transparently.
•= Shared services between the Java and PL/SQL execution engines: Within the server, the Java Virtual
Machine and the PL/SQL engine already share a number of common services, to provide users with
optimal performance and scalability.
For more details, please refer to [5], an Oracle technical white paper on “Multi-Language Program
Development in Oracle8i”.
The Probe family of APIs provides a broad range of low level services that can help tools builders
build development environments to debug and monitor PL/SQL applications. The first of these
APIs was the debugger API, which has been available since PL/SQL Release 2.3.4. The Oracle
Procedure Builder product integrates with this, to provide a PL/SQL debugger.
New APIs have been added to Oracle8i PL/SQL Release 8.1, to support PL/SQL profiling, code
coverage, and profiling. These APIs are being made available to tools vendors.
For information on how to use the PL/SQL cartridge with the Oracle Application Server to develop
Web applications using PL/SQL, please refer to [4], an Oracle technical white paper on “Developing
Web Applications Using PL/SQL”.
SUMMARY
Oracle8i PL/SQL Release 8.1 includes many features that improve ease of use and application
development. In addition, this new release includes a number of performance improvements. For
details, please refer to [3], an Oracle technical white paper on “Techniques for Tuning PL/SQL
Applications”. For more details on all the new functionality, please refer to [7] “Oracle8i
Application Developer’s Guide - Fundamentals, Release 8.1.5”, [8] “Oracle8i Concepts, Release
8.1.5”, and [9] “PL/SQL User’s Guide and Reference, Release 8.1.5”. Oracle intends to fully support,
enhance, and extend PL/SQL, to make it even easier to use, perform even faster, and, in general,
much better.
The authors of this paper, Usha Sangam (editor), Guhan Viswanathan, Dave Alpern, Kannan
Muthukkaruppan, Neil Le, Ron Decker, Radhakrishna Hari and Chandrasekharan Iyer thank Shirish
Puranik and Ashok Swaminathan for their review comments and suggestions for improvements to
this paper.
REFERENCES
1. Oracle8i PL/SQL - New Features and Future Directions - an Oracle Technical White Paper,
February 1999
2. Native Dynamic SQL: A Better Alternative to DBMS_SQL - an Oracle Technical White Paper,
January 1999
3. Techniques for Tuning PL/SQL Applications - an Oracle Technical White Paper, November 1998
4. Developing Web Applications Using PL/SQL - an Oracle Technical White Paper, January 1999
Worldwide Inquiries:
+1.650.506.7000
Fax +1.650.506.7200
http://www.oracle.com/