Oracle9i PL SQL A Developer S Guide 1st Edition Bulusu Lakshman (Auth.) Download
Oracle9i PL SQL A Developer S Guide 1st Edition Bulusu Lakshman (Auth.) Download
https://ebookgate.com/product/oracle9i-pl-sql-a-developer-s-
guide-1st-edition-bulusu-lakshman-auth/
https://ebookgate.com/product/advanced-oracle-pl-sql-developer-s-
guide-2nd-edition-saurabh-k-gupta/
ebookgate.com
https://ebookgate.com/product/oracle-pl-sql-programming-animal-
guide-5th-edition-steven-feuerstein/
ebookgate.com
https://ebookgate.com/product/beginning-pl-sql-1st-edition-donald-
bales/
ebookgate.com
https://ebookgate.com/product/oracle-pl-sql-for-dummies-1st-edition-
michael-rosenblum/
ebookgate.com
Learning Oracle PL SQL 1st ed Edition Bill Pribyl
https://ebookgate.com/product/learning-oracle-pl-sql-1st-ed-edition-
bill-pribyl/
ebookgate.com
https://ebookgate.com/product/unlocking-android-a-developer-s-
guide-1st-edition-frank-ableson/
ebookgate.com
https://ebookgate.com/product/postgresql-developer-s-guide-1st-
edition-ahmed/
ebookgate.com
https://ebookgate.com/product/expert-pl-sql-practices-for-oracle-
developers-and-dbas-1st-edition-john-beresniewicz/
ebookgate.com
https://ebookgate.com/product/head-first-sql-your-brain-on-sql-a-
learner-s-guide-head-first-1st-ed-edition-lynn-beighley/
ebookgate.com
Oracle9i PLISOL
- :
A Developer's Guide
BULUSU LAKSHMAN
Apress'"
Oracle9i PL/SQL: A Developer's Guide
All rights reserved. No part ofthis work may be reproduced or transmitted in any form or byany
means, electronic or mechanical, induding photocopying, recording, or by any information
storage or retrieval system, without the prior written permission of the copyright owner and the
publisher.
Trademarked names may appear in this book. Rather than use a trademark symbol with every
occurrence of a trademarked name, we use the names only in an editorial fashion and to the
benefit of the trademark owner, with no intention of infringement of the trademark.
Distributed to the book trade in the United States by Springer-Verlag NewYork, Inc., 233 Spring
Street, 6th Floor, NewYork, NY 10013 and outside the United States by Springer-Verlag GmbH &
Co. KG, Tiergartenstr. 17,69112 Heidelberg, Germany.
For information on translations, please contactApress directly at 2560 9th Street, Suite 2l9,
Berkeley, CA 94710. Phone 510-549-5930, fax 510-549-5939, email [email protected], or visit
http://www.apress.com.
The information in this book is distributed on an "as is" basis, without warranty. Although every
precaution has been taken in the preparation of this work, neither the author nor Apress shall
have any liability to any person or entity with respect to any loss or damage caused or alleged to
be caused directly or indirectly by the information contained in this work.
The source code for this book is available to readers at http://www.apress.comin the Source
Code/Downloads section.
With love to my parents, Professor B.S.KR. Somayajulu and Smt. B. Sita.
Contents at a Glance
Foreword ............................................................ .xiii
v
Part Four: PL/SQL with Java and the Web ...................... .507
Chapter 13 Floating in Java .................................... .509
vii
Contents
viii
Contents
ix
Contents
Part Four: PLISQL with Java and the Web ............ .507
x
Contents
xi
Foreword
I HAD ALREADY READ a couple of chapters of Bulusu Lakshman's book on Orade9 i
PLlSQL when I was asked to write this Foreword. I immediately accepted
because of the strong impression the book made on me.
There have been several PLlSQL titles published over the years, but I think
Bulusu's book is the best Orade PLlSQL book ever written. There are several rea-
sons for this emphatic dedaration, one of them being that Oracle9i PLlSQL:
A Developer's Guide is the most comprehensive book on the subject, period. It
takes readers from the basics to the most complex techniques. Moreover, what's
most important is that Bulusu doesn't just "cover" the topics-he leads readers
step by step through the various techniques. The examples in this book use an
actual Orade9 i database and some sampie schemas, and the data is all right
there for you to leam or verify the techniques. Like mathematics, PLlSQL is an
endeavor in which you need to practice the examples to get good at the game.
You don't have to know any PLlSQL to start using this book, and ifyou persevere
and try the multitude of tested examples Bulusu provides, you might very weIl
become an expert in PLlSQL in short order.
Bulusu has set hirns elf a lofty goal: to write a comprehensive book on PLlSQL
that is technically complete and accessible to even the beginn er. I am glad to
confirm that Bulusu has succeeded in achieving his goal. Orade professionals at
various career stages, from fledgling newbies to seasoned veterans, will find this
book a treasure trove of useful examples and lucidly explained concepts.
The very first chapter, which intro duces PLlSQL, sets the tone for the book,
and Bulusu carefully and painstakingly builds on this chapter. The next chapter
(Chapter 2) covers cursors, and it provides the best treatment of the subject
that I've seen in any book. The chapter on record types and index-by tables
(Chapter 3) is also exceIlent. Bulusu takes care throughout the book to provide
useful information to his readers. Two examples of this are the tips for PLlSQL
error messages in Chapter 4 and the performance-improving tips in Chapter 15.
The chapter on triggers (Chapter 6) is another good example of how this book
stands out from many of the other titles on the market: You not only leam about
aIl there is to leam ab out creating and deploying triggers, but you also leam
about the dba_tables that contain pertinent information for managing triggers,
details about enabling and disabling triggers, and the privileges you need to be
able to use triggers. Typically, authors explain a concept, provide some examples,
and move on, but Bulusu consistently endeavors to provide a complete treat-
me nt of each topic so readers can not only leam what the concept means, but
also implement the concept successfully in the real world.
xiii
Foreward
Orade provides its own manuals, but they do not always offer a fruitful way
for developers to learn new and complex techniques. Bulusu takes you beyond
what any Orade manual can show you ab out using the innovations in Orade9 i
PLlSQL. Chapter 7, which covers native dynamic SQL, is a good example of this.
The chapter is a very useful yet simple and systematic tutorial-like introduction
to complex topics such as dynamic bulk SQL and dynamic PLlSQL. Bulusu cov-
ers a lot of ground in this book and indudes solid treatments of topics such as
Java stored procedures (Chapter 13) and PLlSQL and the Web (Chapter 14). The
latter chapter shows you how to Web-enable your PLlSQL code. Chapter 15 alone
makes the book an indispensable volume for any professional who uses or
intends to use PLlSQL-in it, Bulusu provides the most comprehensive set of
PLlSQL performance-enhancing hints and guidelines that you can find any-
where. And Bulusu's chapter on PLlSQL co ding standards (Chapter 16) contains
the most comprehensive co ding guidelines that you can find. In fact, most
PLlSQL books give short shrift to this very important part of PLlSQL implemen-
tation. It is chapters like these that make Bulusu's book astandout among the
current crop of Orade books.
Bulusu's book is an important addition to the Orade9 i literature, and it will
remain a favorite book on many a developer's desk in the years to come. Enjoyyour
voyage through the new Orade9iPLlSQL world, with Bulusu's book byyour side!
Sam Alapati
October 4,2002
xiv
About the Author
Bulusu Lakshman is an experienced Orade developer with
more than 10 years of extensive software design and develop-
ment experience in using Orade and its related tools, induding
PL/SQL and Java. He's the author of Orade Developer Forms
Techniques and Orade and Java Development, both of which
have been weil received. He also has an Orade Master creden-
tial from Orade Corporation, is an Orade Certified Professional (OCP) application
developer, and is a double honors graduate in computer science and engineering
and mathematics. He has presented at numerous international and national
conferences on Orade and its tools and published artides in various technical
magazines and journals in the United States and the United Kingdom. He is cur-
rently employed by Compunnel Software Group Inc., a leading technical
consulting firm based in New Jersey.
xv
About the Technical
Reviewer
Martin W.P. Reid is currently employed at Queen's University
Belfast, where he is responsible for staff and student com-
puter training, teaching everything from Windows to Web
authoring to database design. He is also employed by the
faculty of computer science as apart-time lecturer in database
design and teaches several courses using Microsoft Access
for other areas of the university.
Martin particularly enjoys the contact with staff and students, and one of his
biggest thrills was when two of his part-time students-who had previously never
been near a database-started to work as developers.
Martin is married with six children. He credits his wife, Patricia, with keeping
the whole show on the road while he plays with databases.
xvi
About the Foreword
Writer
Sam R. Alapati is an experienced Orade database administrator who holds the
Orade OCP DBA and the Hewlett Packard UNIX System Administrator certifi-
cations. He currently manages Orade databases at the Boy Scouts of America's
National Office in Los Colinas, Texas. Sam has been dealing with databases for
a long time, induding the Ingres RDBMS in the mid-1980s. In addition, he is weIl
versed in the Sybase and IBM DB2 database management systems. Sam has also
taught Orade DBA dasses for system administrators.
xvii
Acknowledgments
I THANK MYWIFE, Anuradha, for her eonsistent endeavor in ereating a positive
atmosphere throughout the writing period of my book.
I thank my brother B. Rama for his eonstant eneouragement throughout.
I thank Mr. Paul Wu, direetor of MIS at Assoeiated Press, the dient eompany
for whieh I am eonsulting, for providing me with neeessary resourees to write
this book.
I thank my friends V. Satya Prasad and Devendra Trivedi for their help in
preparing part of the manuseript and providing valuable suggestions.
I thank Mr. Vinayak Rampalli and Mr. Rakesh Shah of Compunnel Software
Group Ine. for letting me use the name of themail server as an example for one of
the programs in my book.
I thank Apress' publisher, Gary Cornell, and the various editors at Apress for
their help in editing and publishing my book.
xix
Introduction
Introduction
ORACLE9IIS THE major relational database management system (RDBMS) for
xxi
Introduction
xxii
Introduction
xxiii
Part One
Introduction
CHAPTER 1
Introduction to PLISOL
-
ORACLE FIRST INTRODUCED Pro ce dural Language/Structured Query Language
(PLlSQL) in version 6.0 ofits relational database management system (RDBMS).
As its RDBMS evolved, Orade made developmental changes to the PLlSQL lan-
guage by introducing new features and enhancing existing features. As of
Orade9i, the version of PLlSQL is PLlSQL 9.2 or 9.0 depending on whether it
is Orade9 i Release 2 (9.2.x) or Orade9 i Release 1 (9.0.x). In this book, I refer to
both versions collectively as PLlSQL 9i.
PLlSQL incorporates third-generation language (3GL) structures otherwise
unavailable in Structured Query Language (SQL). SQL is a fourth-generation lan-
guage (4GL) , meaning it uses constructs and elements that specify "wh at to do"
without having to specify "how to do it." It's a major language for the Orade
RDBMS (as weIl as for other RDBMSs), and it's used for data definition, database
querying, and data manipulation and control. However, there are situations that
demand the use of 3GL constructs, such as conditional or iterative execution of
SQL and the like. This kind of logic and control flow can be achieved only in
a 3GL language such as Java, C++, or C. To accommodate 3GL features, Orade
designed and implemented the PLlSQL language as a procedural extension to
SQL. PLlSQL is integrated with SQL, and both SQL and PLlSQL serve as the
major database server-side languages, with each language complementing the
other. You can also use PLlSQL in dient-side environments.
Table 1-1 offers a comparison ofPLlSQL with other 3GLs such as Java,
C++, andC.
3
Chapter 1
In this chapter, I describe the building blocks of PLlSQL and discuss the
various PLlSQL environments. I also highlight the new features of PLlSQL 9i.
PLlSQL has its roots in ADA, a high-level programming language. The concept
of the PLlSQL block resembles the concept ofblock structure in ADA using
BEGIN and END blocks. PLlSQL shares other features with ADA such as the
syntax "=" used for comparison and ":=" used for assignment, exception han-
dling, and the dedarative syntax of defining stored subprograms.
Over the years, Orade came up with new releases of PLlSQL. With Orade 7.0,
Orade released PLlSQL 2.0, which turned Orade into an active database with
the capability to store business and application logic in the database in the
form of stored procedures, functions, and packages. It also defined the capabil-
ity to dedare programmer-defined re cords and arrays in the form of PLlSQL
tables. PLlSQL 2.1 came into existence with Orade 7.1 and enabled the use of
stored functions in SQL statements. Also, dynamic SQL was introduced in
4
Introduction to PLlSQL
PLlSQL for the first time with the DBMS_SQL package. Orade 7.2 was released
subsequently, and along with it came PLlSQL 2.2. It had the capability to define
binarywrappers for PLlSQL stored subprograms, thus hiding code from other
developers. Also, the DBMS~OB package was introduced, which enabled pro-
grammers to sub mit jobs from within the database. The next release of PLlSQL
was PLlSQL 2.3, which was introduced with Orade 7.3. It enhanced the capabil-
ities of PLlSQL tables with the ability to define new methods, and it also
enabled programmers to access the file system from within the database. File
I/ 0 could be done using the UTL_FILE package.
Orade 8.xwas a major breakthrough in the Orade database history with the
introduction of objects and Java in the database. PLlSQL 8.xwas released along
with Orade 8.x and induded major features such as native dynamic SQL, Java
stored procedures, and system- and schema-level database triggers.
Finally, Orade9i was released and along with it came PLlSQL 9.2 and 9.0, corre-
sponding to Releases 2 and 1 of Orade9 i. This release saw the introduction of
native compilation ofPLlSQL code, enhancement to PLlSQL cursors in the
form of cursor express ions, new data types, enhancements to bulk binding,
pipelined table functions, true inheritance among objects, and more.
Procedural Capabilities
The primary advantage ofPLlSQL is its capability to define and implement 3GL
constructs with embedded SQL statements. Using PLlSQL, you can implement
standard 3GL capabilities, such as support for BOOLEAN data types; sequential,
conditional, and iterative logic; arrays and subprograms; and object-oriented
features. And you can do this side by side with SQL. This support for SQL in
PLlSQL makes PLlSQL powerful. The capability to define 3GL constructs is pri-
marily beneficial when you use PLlSQL in writing applications that mix and
match business logic that's complicated and necessary. Also, PLlSQL is the pri-
mary procedurallanguage for Orade dient-side tools such as Orade Forms and
Orade Reports.
5
Chapter 1
Portability
Pro grams written in PLlSQL are hardware independent and operating system
independent. They are highly portable and work well on any platform where an
Orade server and an integrated development environment (IDE) are installed.
Modularity
PLlSQL enables you to write programs as independent modules that you can
integrate. You can implement this modularity by using features such as proce-
dures, functions, and packages.
Better Performance
PLlSQL offers better performance for three main reasons:
1. Data type conversion isn't needed on input and output. When data is
retrieved into PLlSQL variables or when data is input into Orade using
PLlSQL variables, PLlSQL takes care ofthe appropriate conversion
between the Orade server and PLlSQL by me ans of the same internal
database format. Other 3GL programs written in C++ or Java to access an
Orade database are required to convert data to and from Orade format
to C++/Java format.
6
Introduction to PLlSQL
Object Orientation
PLlSQL provides object-oriented features such as encapsulation, abstraction (the
ability to define abstract data types), data and information hiding, reusability,
inheritance, polymorphism, and dynamic method dispatch. Various features
introduced in PLlSQL, from its earlier versions to PLlSQL 9i, enable object orien-
tation to be defined and implemented in PLlSQL. I discuss these features
throughout this book.
Whatever the type, a PLlSQL block consists of the following sections defined
within it:
7
Chapter 1
• Declaration section: This section begins with the DECLARE keyword and
contains declarations of variables, constants, cursors, and local subpro-
grams. This section is optional for a block.
• Executable section: This section begins with a BEGIN statement and con-
sists of procedurallogic and SQL statements. It ends with an END
statement.
DECLARE
v_item_code VARCHAR2(6);
v_item_descr VARCHAR2(20);
BEGIN
v_item_code := 'ITM10l';
v_item_descr := 'Spare parts';
INSERT INTO items_tab VALUES (v_item_code, v_item_descr);
EXCEPTION WHEN OTHERS THEN
dbms_output.put_line(SQLERRM);
END;
/
The program in Listing 1-1 declares two PL/SQL variables named v_item_code
and v_item_descr to store the item code and description, respectively. It initializes
these two variables with specific values and then inserts into the items_tab table
with values held in these two variables. Any errors that occur in the process are
8
Introduction to PLlSQL
output to the screen buffer with a corresponding error message. This is done
by a call to a special procedure named dbms_output.puUine, a packaged proce-
dure that outputs to the SQL*Plus screen with a corresponding message passed as
input to it. To make this procedure work, the SQL*Plus environment variable
SERVEROUTPUT should be set to ON. Here's the code to do this in SQL*Plus:
Listing 1-2. The Modijied PLlSQL Code That Uses a Nested Block
DECLARE
v_item_code VARCHAR2(6);
v_item_descr VARCHAR2(20);
v_num NUMBER(l);
BEGIN
v_item_code := 'ITM10l';
v item descr := 'Spare parts';
BEGIN
SE LECT 1
INTO v num
FROM i tems tab
WHERE item code = v_item_code;
EXCEPTION
WHEN NO DATA FOUND THEN
v_num := 0;
WHEN OTHERS THEN
dbms_output.put_line('Error in SELECT: 'IISQLERRM);
RETURN;
END;
IF (v_num = 0) THEN
INSERT INTO items tab VALUES (v_item_code, v_item_descr);
END IF;
dbms_output.put_line('Successful Completion' ) ;
EXCEPTION WHEN OTHERS THEN
dbms_output.put_line(SQLERRM);
END;
/
9
Chapter 1
• There's an inner PLlSQL block nested in the outer PLlSQL block. This
inner block begins with the second BEGIN statement.
• Each PLlSQL block starts with a BEGIN statement and ends with an END
statement.
• Any PLlSQL block can have an optional dedaration section. In the preced-
ing example, the outer block has a dedaration section but the inner block
doesn't.
• Each PLlSQL block has its own exception handling section. !t's up to the
program to handle exceptions in each block. Any uncaught exceptions in
the nested block propagate to the exception handler in the immediate
outer block. If an exception isn't caught at all in any of the blocks, it results
in an unhandled exception, and control transfers out ofthe pro gram.
• Support of the BOOLEAN data type, reference cursor (REF CURSOR) types,
and user-defined subtypes (not available in the Orade server)
10
Introduction to PLlSQL
• Iterative constructs such as loops (LOOP ... END LOOB FOR LOOB and
WHILE LOOP), single-dimensional arrays, and re cords
• Object-oriented features
Conditional Constructs
A conditional construct executes a set of statements based on a condition being
true. It's specified by the IF statement. Here's the syntax:
IF (conditionl) THEN
[actionl]
ELSIF (condition2) THEN
[action2]
ELSIF (condition3) THEN
[action3]
ELSE
[actionN]
END IF;
DECLARE
a number 50;
b number -20;
BEGIN
IF (a>b) THEN
dbms_output.put_line('A is greater than B');
11
Chapter 1
You can nest the IF statement. However, good pragramming practice recom-
mends that you avoid nested IFs wherever possible and use ELSIF instead.
Iterative Constructs
Iterative constructs are specified by means of loops. There are three types of
loops that perform repetitive contral flow, namely the simple LOOp, the numeric
FOR LOOp, and the WHILE LOOP. Each of these loops ends with an END LOOP
statement.
LOOP
[statementl]
[statement2]
12
Introduction to PLlSQL
In the preceding code, statementl, statement2, and so forth refer to the exe-
cutable statements constituting the body of the loop, and condition refers to the
EXIT condition for the loop when the contral exits the loop. If the EXIT condition
isn't specified, the execution would result in an infinite loop. Here's an example of
the simple LOOP:
DECLARE
line_length NUMBER:= 50;
separator VARCHAR2(1) := '=';
actual_line VARCHAR2(150);
i NUMBER 1;
BEGIN
LOOP
actual line := actual line I I separator;
EXIl WHEN i = line_length;
i: = i + 1;
END LOOP;
DBMS_OUlPUl.PUl_LINE(actual_line);
END;
/
This pragram displays a line made of equal signs (=) whose length equals 50.
The index of the loop has to be explicitly defined, initialized, and incre-
mented. The body of the loop must have an explicit EXIT condition specified.
You can write the same simple LOOP as a numeric FOR LOOP or a WHILE LOOP.
The FOR LOOP has the following syntax:
END LOOP;
In the preceding code, index refers to a variable that increments the count
for the duration of the loop, and initialval and finalval refer to the initial and final
values that the index will take when looping. The difference between these two
13
Chapter 1
values plus 1 gives the number oftimes the contral repetitively executes the
statements in the body of the loop-that is, between FOR LOOP and END LOOP.
Here's the line display example written using a numeric FOR LOOP:
DECLARE
line_length NUMBER:= 50;
separator VARCHAR2(1) := '=';
actual_line VARCHAR2(150);
BEGIN
FOR idx in 1.. 1ine_length LOOP
actual_line : = actual_line 11 separator;
END LOOP;
DBMS_OUTPUT.PUT_LINE(actual_line);
END;
/
You dont need to declare the index of the FOR LOOP. Also, you don't need to
initialize the index, and you need not increment the index for each iteration of
the loop. You don't need an EXIT condition for the loop.
END LOOP;
DECLARE
line_length NUMBER:= 50;
separator VARCHAR2(1) := '=';
actual_line VARCHAR2(150);
idx NUMBER := 1;
14
Introduction to PLlSQL
BEGIN
WHILE (idx<=line_1ength) LOOP
actua1_1ine := actua1_1ine I I separator;
idx := idx +1 ;
END LOOP;
DBMS_OUTPUT.PUT_LINE(actua1_1ine);
END;
/
The index of the WHILE LOOP must be dedared and initialized. The index
should be incremented programmatically. The condition is evaluated first, and if
it results to be TRUE, then only the body of the loop is executed. There's no need
for an explicit EXIT condition for the loop.
PL/SQL Environments
To develop and execute 3GL programs, you need a development environment in
which you can write and execute the programs. This is called an integrated devel-
opment environment, or IDE. Most popular 3GLs such as Java, C++, and so forth
provide such IDEs as IBM's Visual Age for Java, Visual C++, and so on.
There are typically three types of environments necessary for any program-
ming language, and PLlSQL is no exception:
1. Development environment
2. Execution environment
3. Debugging environment
15
Chapter 1
)(
Conn.et.d to :
Or.el.91 Ent.rprl" Edition R.l •••• 9 . 2 .8 1 8 - Produetlon
Wlth th. Partltl0nlng . OL P nd Orael. Data Hlnlng options
JS.ru.r R.I.ae. 9 2 eIe - Produetl0n
SOL > I
Grade Procedure Builder is more of a GUI tool and has a more powerful
debugging capability than SQL*Plus. For instance, you can set breakpoints and
you can define the functions of step in, step into, and step out to debug a PLlSQL
subprogram. Figures 1-2 and 1-3 show the typical Procedure Builder develop-
ment and debugging environments, respectively. The debugging environment
has certain breakpoints to track the execution of pro grams. Breakpoints are
points of control in a PLlSQL pro gram where execution ofthe program pauses
and the user can interactively know the value of runtime variables and decide
whether to halt execution by stepping out of the pro gram or resume execution by
stepping into the code.
16
Introduction to PLlSQL
h LOOP
Illp_lIe.,.u Ot:
LIHI: 9
17
Chapter 1
PLlSQL 9i and broadly categorizes them into two groupS: namely, non-object-
oriented features and object-oriented features. Details of each feature are
explained throughout the book.
CASE Statement
Oracle9 i intro duces the CASE statement as an improved alternative to the IF ...
ELSIF ... statement. The use of CASE statement provides more compact code
and increased usability on the part of the programmer, and increased readability
on the part of the code reviewer. Here's the syntax of the CASE statement:
(ASE selector
WHEN valuel THEN actionl;
WHEN value2 THEN action2;
WHEN value3 THEN action3;
ELSE actionN;
END (ASE;
(ASE report_choice
WHEN 1 THEN p_proc_reportl;
WHEN 2 THEN p_proc_report2;
WHEN 3 THEN p_proc_report3;
18
Introduction to PLlSQL
IF (report_choice=l) THEN
p_proc_reportl;
ELSIF (report_choice=2) THEN
p_proc_report2;
ELSIF (report_choice=3) THEN
p_proc_report3;
ELSIF (report_choice=4) THEN
p_proc_report4;
ELSE
dbms_output.put_line('Invalid option. ');
END IF;
CASE Expression
You can use the CASE construct in an expression and assigned it to a variable to
generate a value. Here's the syntax of the CASE expression:
ELSE assigned_valueN;
END (ASE;
19
Chapter 1
temp (ASE i
WHEN o THEN 'Zero'
WHEN 1 THEN 'One'
WHEN 2 THEN 'Two'
WHEN 3 THEN 'Three'
WHEN 4 THEN 'Four'
WHEN 5 THEN 'Five'
WHEN 6 THEN 'Six'
WHEN 7 THEN 'Seven'
WHEN 8 THEN 'Eight'
WHEN 9 THEN 'Nine'
ELSE
NULL
END;
The searched CASE statement is a variant of the CASE statement and has no
selector defined. Instead of testing on the equality of the selector, the searched
CASE statement tests a boolean expression for a truth value. Here's the syntax of
the searched CASE statement:
(ASE
WHEN (boolean_condition1) THEN action1;
WHEN (boolean_condition2) THEN action2;
WHEN (boolean_condition3) THEN action3;
ELSE actionN;
END (ASE;
(ASE
WHEN (report_choice 1) THEN p_procJeport1;
WHEN (report_choice 2) THEN p_procJeport2;
WHEN (report_choice 3) THEN p_proc_report3;
WHEN (report_choice = 4) THEN p_procJeport4;
ELSE dbms_output.put_line('Invalid option. ');
END (ASE;
20
Introduction to PLlSQL
TIP Always specify the ELSE part explicitly when you use
a searched CASE statement.
The searched CASE expression is a variant of the CASE expression, and it omits
the selector. Like the searched CASE statement, it tests for the truth value of
a boolean condition instead of the equality of a single selector common for all
WHEN clauses. !t's used in assignment statements. Here's the example of the pre-
viously presented CASE expression rewritten using a searched CASE expression:
temp := (ASE
WHEN (i=O) THEN 'Zero'
WHEN (i=l) THEN 'One'
WHEN (i=2) THEN 'Two'
WHEN (i=3) THEN 'Three'
WHEN (i=4) THEN 'Four'
WHEN (i=S) THEN 'Five'
WHEN (i=6) THEN 'Six'
WHEN (i=7) THEN 'Seven'
WHEN (i=8) THEN 'Eight'
WHEN (i=9) THEN 'Nine'
ELSE
NULL
END;
declare
a number 20;
b number -40;
string varchar2(SO);
21
Chapter 1
begin
string ca se
when (a>b) then 'A is greater than B'
when (a<b) then 'A is less than B'
else
'A is equal to B'
end;
dbms_output.put_line(string);
end;
I
A is greater than B
PL/SQL procedure successfully completed.
Cursor Expressions
Oracle9 i has incorporated the facility to nest cursors in PLlSQL cursor declar-
ations in the form of cursor express ions. Cursor expressions eliminate the use of
declaring and using multiple cursors, and hence result in a more effective opti-
mization scheme by the SQL engine (only one SQL statement) than in the case of
multiple cursors (multiple SQL statements). Also, cursor expressions eliminate
the use of the complicated joins involved in SQL SELECT statements. As a third
benefit, Oracle9 i removes the limitation of using cursor expressions in SQL
embedded in PLlSQL code. Nowyou can use cursor expressions as part of
PLlSQL cursors. Also, when you use dynamic SQL, you can use and fetch cursor
expressions into REF cursor variables. In this case, they support the complex
binds and defines needed for REF cursors. This is not supported by DBMS_SQL.
22
Introduction to PLlSQL
Associative Arrays
23
Chapter 1
dbms_transform
dbms_flashback
dbms_logmnccdc_publish
dbms_logmnccdc_subscribe
24
Introduction to PLlSQL
dbmsjedefinition
utLencode
In addition, Oracle9ihas three new special SQL data types that enable
dynamic encapsulation of and access to type descriptions, data instances, and
sets of data instances of any other SQL type, including object and collection
types. In addition, you can use these three special types to create anonymous
(i.e., unnamed) types, including anonymous collection types. The three special
types are SYS.ANYTYPE, SYS.ANYDATA, and SYS.ANYDATASET.
Of the enhanced packages, the UTL_FILE package, which provides the API
for file input! output (1/0), also has some enhancements with regard to enabling
opening, reading, and writing files in Uni code format. And as of Oracle9 i
Release 2, the UTL_FILE package has the API to read and write binary data and
perform functions on files such as autoflush, seek, delete, copy, and rename from
within PLlSQL. The UTL_HTTP package has new features to support authentica-
tion, cookies, and sending large messages and persistent connections.
The new SQL features, such as the new date/time data types TIMESTAMP and
INTERVAL (and their variants), the MERGE statement, and the nullif and
coalesce functions, are automatically available for use in PLlSQL 9i.
You can use TIMESTAMP with two variants such as TIMESTAMP WITH TIME
ZONE and TIMESTAMPWITH LOCAL TIME ZONE. TIMESTAMP offers ahigher
level of granularity in the time component in addition to the capability to deal
with time zones. You can use INTERVAL with two variants: INTERVAL YEAR TO
MONTH and INTERVAL DAYTO SECOND. Also, Oracle9ihas defined a set of
built-in functions associated with TIMESTAMP and INTERVAL, and you can
invoke all of these functions in PLlSQL.
The MERGE statement enables you to combine the insert and update
functions into a single operation.
25
Chapter 1
Miscellaneous Enhancements
Other new features (in addition to the ones I described in the previous sections)
that are significant to SQL and/ or PL/SQL performance and usability are as
follows:
• Integration oJthe SQL and PLlSQL parser: The integration of SQL and
PL/SQL is tighter with SQL and PL/SQL sharing a parser. This enables fea-
tures implemented in SQL to be automatically available in PL/SQL.
• Autogeneration oJPLlSQL call specs Jor Java stored procedures while loading
Java classes into the database: This feature is available as of Oracle9 i
Release 2. I discuss this feature further in Chapter 13.
Oracle9iPL/SQL allows dynamic bulk binding, which is the use ofthe BULK
COLLECT construct for SELECT statements, the FORALL ... USING
construct for INSERT, and the UPDATE and DELETE statements with the
EXECUTE IMMEDIATE construct so that bulk binding can be done with native
dynamic SQL in PL/SQL. Dynamic bulk binding is also supported for implicit
query in a DML statement via the RETURNING keyword.
26
Other documents randomly have
different content
He was silent a moment, realizing there was nothing to be gained
by attempting to deceive her. How, when or where she learned of
these matters he never knew; but she knew perhaps more than he
did of what was happening in Fukui.
“Even if it is so,” he finally said, “and the samourai too are gone,
you have nothing to fear. Less than a week ago a courier brought
word to me from Tokio. I am expecting friends in Fukui very shortly
now.”
“Frien?” she repeated wistfully. “Like unto you, kind Tojin-san?”
“Yes—white men, and Japanese, too, for that matter. I have good
friends in Tokio. They are coming here to see you, my child.”
“Alas!” she said, shrinking slightly from him, “Why do they come?”
“I asked them to come,” he said, very gravely. “I feel I am right,
and that by a simple operation we will be able to make you see, as
other people do, my child.”
The word appeared to trouble her.
“I see already, Tojin-san,” she said.
“What do you see, Tama?” he asked her huskily.
The words came floodingly, tumultuously to her lips. The misty
eyes were blue as the sea and as beautiful.
“I see thee, Tojin-san. Thou art beautiful ad my sight, lig’ unto the
gods.”
A look of suffering left its mark upon the face of the Tojin. He
gazed at the kindling face of the girl before him, and the old
strangling, yearning emotion swept over him.
“Give me more sight—if it is your honorable wish,” she said, “bud
already I see—I know!” She pressed her fingers impetuously to her
eyes.
“I see the light—the dark. It is a worl’ of shadows on my eyes,
and shadows are lig’ unto our dream—mos’ beautiful of all!”
His voice was firm, almost solemn.
“You have been wandering around in a black wilderness all of your
life; you do not know what it is, my poor little one, to see the sun!
But, with God’s good help, I am going to lead you out of the
wilderness—into the light!”
“You are the light!” she said, throbbingly, and slipped to her
knees, putting her face against his hand.
Something bounded against the wall and came whistling through
the shoji. It grazed the cheek of the kneeling fox-woman, and
imbedded itself against the woodwork of the opposite wall. She put
up her hand with a quick, startled movement, but though she turned
a questioning, fearful face upon the great Tojin, she could not see
how deathly white he had become. He bent suddenly above her.
“Make me a promise. Repeat after me, that no matter what might
befall us, you will remain with me—you will not desert me!”
With her face pressed against his hand, her eyes fervently closed,
she repeated the words as a veritable prayer.
XVI
ebookgate.com