Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Download
Standard view
Full view
of .
Look up keyword
Like this
1Activity
0 of .
Results for:
No results containing your search query
P. 1
chapt~18

chapt~18

Ratings: (0)|Views: 3 |Likes:
Published by Arsalan Ahmed
chapt~18
chapt~18

More info:

Published by: Arsalan Ahmed on Dec 26, 2012
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

09/18/2014

pdf

text

original

 
PL/SQL
P
rocedural Language for Structured Query Language(PL/SQL) has many uses. Entire books have beendevoted to PL/SQL alone. This chapter contains manyexamples of PL/SQL code and discusses the basicconstruction techniques of writing PL/SQL programs.PL/SQL has been a part of the Oracle database engine formany years. As its name implies, PL/SQL gives you aprocedural backdrop in which to couch your SQL commands.Other chapters in this book discuss techniques for workingwith PL/SQL. The “Procedures, Packages, Functions, andTriggers” section in Chapter 19 contains specific instructionsand examples to construct the four important kinds of Objectsusing PL/SQL. Chapter 25 has an example PL/SQL procedurethat uses Oracle8 Objects.Each section in this chapter has sample code that runs usingthe sample Tables included on the CD-ROM. Follow theinstructions in Appendix B to install the Tables in yourdatabase.
Writing PL/SQL Code
The basic structure of a PL/SQL follows:
DECLAREdeclare blockBEGINbody blockEXCEPTIONexception blockEND;
The
declare block
contains definitions of variables andcursors. The
body block
contains the executable code—youcan break this code into subblocks. The
exception block
contains error handling code executed only if you encounter apredefined exception.
On theCD-ROMCross-Reference
1818
C H A P T E R
3 3 3 3
In This Chapter
Writing PL/SQL codeUsing declaredvariablesHandling errorsUsing PL/SQL tocreate HTMLCreating cursorsExamining loopand
IF
statements
3 3 3 3
 
444
Chapter 18
3
PL/ SQL
The program can be simple and short. In fact, the program body is the onlyrequired part; all other parts are optional. PL/SQL is used inside the database in theform of database Triggers. You can write application Triggers using PL/SQL inOracle’s Forms 4 Package. Database design toolssuch as Oracle Case and LogicWorks ERwingenerate PL/SQL code for you to enforce business rules andConstraints.You can write and execute PL/SQL within SQL*Plus or SQL Worksheet.PL/SQL can execute
SELECT
,
INSERT
,
UPDATE
, or
DELETE
commands.Data Definition Language (DDL) commands are illegal in PL/SQL. There are specialways around this restriction, using Dynamic SQL and the DBMS_SQL Package, but Ido not cover these techniques in this book.The only other SQL commands Oracle8 allows within a PL/SQL program aretransaction control statements such as
COMMIT
and
ROLLBACK
.In addition to SQL commands, you can create and assign variables, perform
IF-THEN-ELSE
logic, use loop and
GOTO
structures, and make comparisons.
Declared Variables
You can declare variables in two ways:
doeff8returri5tytvghjg
3
Explicit.
Declare explicit variables in the
DECLARE
block of the program. Thesyntax of the explicit variable declaration follows:
variable_name datatype [CONSTANT] [NOT NULL][ [:= | DEFAULT] initvalue] ;
Replace
variable_name
with the actual variable name. Replace
datatype
with the actual datatype of the variable (such as
VARCHAR2
). Replace theoptional parameter
initvalue
with a starting value for the variable. Use the
CONSTANT
parameter when a variable is assigned a value and never changes.For example, the following command declares a variable named
LIBRARY_CARD_ID
, which has a null value as the initial value:
LIBRARY_CARD_ID VARCHAR2(20);
The following command declares a variable named
BOOKS_OUT
, which isinitialized to zero:
BOOKS_OUT NUMBER := 0;
3
Implicit.
Use an assignment in the
BODY BLOCK
of the program to create animplicit variable. When you fetch a cursor using the cursor
FOR LOOP
, you
 
445
Chapter 18
3
Declared Variables
define an implicit variable for the record. See the following “Cursors” sectionfor example code.The following simple PL/SQL program is a Trigger for adding the current date andtime to a Column in a database:
- -trigger_one.sqlCREATE OR REPLACE TRIGGER SEAPARK.BEF_INS_TANKBEFORE INSERT ON SEAPARK.TANKREFERENCING OLD AS old NEW AS newFOR EACH ROWBEGINDECLARETODAY_DATE DATE;BEGINSELECT SYSDATE INTO TODAY_DATE FROM DUAL;:new.LAST_CHANGE_DATE := TODAY_DATE;END;END;
The variable
:new.LAST_CHANGE_DATE
is an implicit variable representing thevalue to be inserted into the Column
LAST_CHANGE_DATE
in the Table
TANK
.You can compare the value of a Column prior to an update when you use the
:old
prefix on a Column in a Trigger that fires before an update. For example, thefollowing Trigger checks the value of
APPROVED_FLAG
. If
APPROVED_FLAG
has beenchanged to
, a date is added to the
APPROVED_DATE
Column and the Oracle User isadded to the
APPROVED_BY_NAME
Column.
- - trigger_two.sqlCREATE OR REPLACE TRIGGER SEAPARK.BEF_UPD_TANKBEFORE UPDATE ON SEAPARK.TANKREFERENCING OLD AS old NEW AS newFOR EACH ROWBEGINDECLARETODAY_DATE DATE;BEGINSELECT SYSDATE INTO TODAY_DATE FROM DUAL;IF :new.APPROVED_FLAG = ‘Y’and ( NVL(:old.APPROVED_FLAG, ‘?’) <> ‘Y’ ) THEN:new.APPROVED_DATE := TODAY_DATE;:new.APPROVED_BY_NAME := User;END IF;END;END;/show errors
The last two lines enable you to compile and view any errors when using SQL*Plusor SQL Worksheet to create and execute your PL/SQL commands.
Tip

You're Reading a Free Preview

Download
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->