By Steven Feuerstein
With
Bill Pribyl
Cover | Table of Contents | Colophon
1 DECLARE 2 l_book_count INTEGER; 3 4 BEGIN 5 SELECT COUNT(*) 6 INTO l_book_count 7 FROM books 8 WHERE author LIKE '%FEUERSTEIN, STEVEN%'; 9 10 DBMS_OUTPUT.PUT_LINE ( 11 'Steven has written (or co-written) ' || 12 l_book_count || 13 ' books.'); 14 15 -- Oh, and I changed my name, so... 16 UPDATE books 17 SET author = REPLACE (author, 'STEVEN', 'STEPHEN') 18 WHERE author LIKE '%FEUERSTEIN, STEVEN%'; 19 END;
|
Oracle version
|
PL/SQL version
|
Characteristics
|
|---|---|---|
|
Oracle6
|
1.0
|
Initial version of PL/SQL, used primarily as a scripting language in
SQL*Plus (it was not yet possible to create named, reusable, and
callable programs) and also as a programming language in SQL*Forms 3.
|
|
Oracle7
|
http://oracle.oreilly.com) for
much more complete information.
CREATE FUNCTION wordcount (str IN VARCHAR2) RETURN PLS_INTEGER AS declare local variables go here BEGIN implement algorithm here END; /
Function created.
CREATE OR REPLACE FUNCTION wordcount (str IN VARCHAR2) RETURN PLS_INTEGER AS same as before
/* File on web: booktitle.fun */ CREATE OR REPLACE FUNCTION booktitle (isbn_in IN VARCHAR2) RETURN VARCHAR2 IS l_isbn books.title%TYPE; CURSOR icur IS SELECT title FROM books WHERE isbn = isbn_in; BEGIN OPEN icur; FETCH icur INTO l_isbn; CLOSE icur; RETURN l_isbn; END; /
SQL> EXEC DBMS_OUTPUT.PUT_LINE(booktitle('0-596-00180-0'))
Learning Oracle PL/SQL
PL/SQL procedure successfully completed.
|
Type
|
Characters
|
|---|---|
|
Letters
|
A-Z, a-z
|
|
Digits
|
0-9
|
|
Symbols
|
~ ! @ # $ % & * ( ) _ - + = | [ ] { } : ; "
' < > , . ? / ^
|
|
Whitespace
|
Tab, space, newline, carriage return
|
$ (dollar sign), _
(underscore), and # (pound sign)
lots_of_$MONEY$ LOTS_of_$MONEY$ Lots_of_$Money$
company_id# primary_acct_responsibility First_Name FirstName address_line1 S123456
1st_year -- Starts with numeral procedure-name -- Contains invalid character "-" minimum_%_due -- Contains invalid character "%" maximum_value_exploded_for_detail -- Name is too long company ID -- Cannot have embedded spaces in name
'Steven' 'steven'
IF 'Steven' = 'steven'
IF salary < min_salary (2003) THEN salary := salary + salary*.25; END IF;
IF salary < min_salary (2003) THEN salary := salary + salary*.25; END IF;
IF salary < min_salary (2003) -- Function returns min salary for year. THEN salary := salary + salary*.25; END IF;
PRAGMA instruction;
<<identifier>>
BEGIN ... <<the_spot>> NULL;
<<insert_but_ignore_dups>>
BEGIN
INSERT INTO catalog
VALUES (...);
EXCEPTION
WHEN DUP_VAL_ON_INDEX
THEN
NULL;
END insert_but_ignore_dups;
<<outerblock>>
DECLARE
counter INTEGER := 0;
BEGIN
...
DECLARE
counter INTEGER := 1;
BEGIN
IF counter = outerblock.counter
THEN
...
END IF;
END;
END;
If the salary is between ten and twenty thousand, then apply a bonus of $1500. If the salary is between twenty and forty thousand, apply a bonus of $1000. If the salary is over forty thousand, give the employee a bonus of $500.
If the user preference includes the toolbar, display the toolbar when the window first opens.
|
IF type
|
Characteristics
|
|---|---|
IF THEN END IF; |
This is the simplest form of the IF statement. The condition between
IF and THEN determines whether the set of statements between THEN and
END IF should be executed. If the condition evaluates to FALSE, the
code is not executed.
|
If the salary is between ten and twenty thousand, then apply a bonus of $1500. If the salary is between twenty and forty thousand, apply a bonus of $1000. If the salary is over forty thousand, give the employee a bonus of $500.
If the user preference includes the toolbar, display the toolbar when the window first opens.
|
IF type
|
Characteristics
|
|---|---|
IF THEN END IF; |
This is the simplest form of the IF statement. The condition between
IF and THEN determines whether the set of statements between THEN and
END IF should be executed. If the condition evaluates to FALSE, the
code is not executed.
|
IF THEN ELSE END IF; |
This combination implements an either/or logic: based on the
condition between the IF and THEN keywords, execute the code either
between THEN and ELSE or between ELSE and END IF. One of these two
sections of executable statements is performed.
|
IF THEN ELSIF ELSE END IF; |
This last and most complex form of the IF statement selects an action
from a series of mutually exclusive conditions and then executes the
set of statements associated with that condition. If
you're writing IF statements like this in
Oracle9i, you should consider using
searched CASE statements instead.
|
CASE expression WHEN result1 THEN statements1 WHEN result2 THEN
GOTO label_name;
<<label_name>>
BEGIN
GOTO second_output;
DBMS_OUTPUT.PUT_LINE('This line will never execute.');
<<second_output>>
DBMS_OUTPUT.PUT_LINE('We are here!');
END;