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 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 Database
release | PL/SQL version | Characteristics |
|---|---|---|
6.0 | 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. |
7.0 | 2.0 | Major upgrade to PL/SQL 1.0. Added
support for stored procedures, functions, packages,
programmer-defined records, PL/SQL tables, and many package
extensions. |
7.1 | 2.1 | Supported programmer-defined
subtypes, enabled the use of stored functions inside SQL
statements, and offered dynamic SQL with the DBMS_SQL package.
With PL/SQL 2.1, you could execute SQL DDL statements from
within PL/SQL programs. |
CREATE FUNCTION wordcount (str IN VARCHAR2)
RETURN PLS_INTEGER
ASdeclare local variables here
BEGIN
implement algorithm here
END;
/
Function created.
CREATEOR 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 -- Doesn't start with a letter
procedure-name -- Contains invalid character "-"
minimum_%_due -- Contains invalid character "%"
maximum_value_exploded_for_detail -- Too long
company ID -- Has embedded whitespace
415, 21.6, 3.141592654f, 7D,
NULL'This is my sentence',
'01-OCT-2006', q'!hello!', NULLINTERVAL '25-6' YEAR TO MONTH,
INTERVAL '-18' MONTH, NULLTRUE, FALSE,
NULLq'!hello!' bears
some explanation. The ! is a
user-defined delimiter, also introduced in Oracle Database
10g; the leading q and the surrounding single quotes tell the
compiler that the ! is the
delimiter, and the string represented is simply the word hello. TO_DATE('01-OCT-2006', 'DD-MON-YYYY')
TO_TIMESTAMP_TZ('01-OCT-2006 00:00:00 -6','DD-MON-YYYY HH24:MI:SS TZH') 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;
PRAGMAinstruction_to_compiler;
<<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 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 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 a condition that is TRUE 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 using any release from
Oracle9i Database Release 1 onwards, you
should consider using searched CASE statements
instead. |