Académique Documents
Professionnel Documents
Culture Documents
Development
Lecture 6 How to Write PL/SQL Code
DECLARE
declaration_statement_1;
[declaration_statement_2;]...
BEGIN
body_statement_1;
[body_statement_2;]...
EXCEPTION
WHEN OTHERS THEN
exception_handling_statement_1;
[exception_handling_statement_2;]...
END;
/
Like SQL, PL/SQL variables must begin with a letter and cannot
contain more than 30 characters
Variables can contain letters, numbers, and the symbols _, $, and
#
Hyphens and blanks are not permitted
You may use no reserve words and they cannot be the same as
any table name
Should be descriptive as possible
Usually are expressed in a combination of upper and lowercase
letters
HTTP 504 Database Design and Development 4 November 2, 2014
PL/SQL Variables and Data Types
This is the structure for a PL/SQL program, there are various areas in an Anonymous
PL/SQL Program or Block
Notice the / on line 9 needed to execute code
A block of comments is
done by using a /* at the
beginning of the block and
a */ at the end of the block
** Exponentiation 2**3 8
* Multiplication 2*3 6
+ Addition 3+2 5
- Subtraction 32 1
- Negation -5 Negative 5
IF...ELSIF...ELSE
CASE...WHEN...ELSE
FOR...IN...LOOP
WHILE...LOOP
LOOP...EXIT WHEN
CURSOR...IS
EXECUTE IMMEDIATE
CONNECT
SET SERVEROUTPUT ON
DBMS_OUTPUT.PUT(string)
Prints the specified string without a line break
DBMS_OUTPUT.PUT_LINE(string)
Prints the specified string followed by a line break
variable_name_1 table_name.column_name%TYPE
max_invoice_total invoices.invoice_total%TYPE
DECLARE
max_invoice_total invoices.invoice_total%TYPE;
min_invoice_total invoices.invoice_total%TYPE;
percent_difference NUMBER;
count_invoice_id NUMBER;
vendor_id_var NUMBER := 95;
BEGIN
SELECT MAX(invoice_total), MIN(invoice_total), COUNT(invoice_id)
INTO max_invoice_total, min_invoice_total, count_invoice_id)
Variable names that were declared
HTTP 504 Database Design and Development 29 November 2, 2014
Syntax fro Setting a Variable to a Literal
Value or an Expression
variable_name := literal_value_or_expression
IF boolean_expression THEN
statement_1;
[statement_2;]...
[ELSIF boolean_expression THEN
statement_1;
[statement_2;]...]...
[ELSE
statement_1;
[statement_2;]...]
END IF;
CASE expression
WHEN expression_value_1 THEN
statement_1;
[statement_2;]...
[WHEN expression_value_2 THEN
statement_1;
[statement_2;]...]...
[ELSE
statement_1;
[statement_2;]...]
END CASE;
CASE
WHEN boolean_expression THEN
statement_1;
[statement_2;]...
[WHEN boolean_expression THEN
statement_1;
[statement_2;]...]...
[ELSE
statement_1;
[statement_2;]...]
END CASE;
row_variable_name table_name%ROWTYPE;
row_variable_name.column_name
Example one
DECLARE
CURSOR LocationCursor IS
SELECT capacity
FROM location;
RoomCapacity location.capacity%TYPE;
BEGIN
OPEN LocationCursor;
FETCH LocationCursor INTO RoomCapacity;
<additional processing statements>
END
Example two
DECLARE
CURSOR LocationCursor IS
SELECT bldg_code, room, capacity
FROM location;
LocationRow LocationCursor%ROWTYPE;
BEGIN
OPEN LocationCursor;
FETCH LocationCursor INTO LocationRow;
<additional processing statements>
END;
Recall that the %TYPE data type assumes the same data
type as a specific database table field and is declared using
the format:
<tablename>.<fieldname>%TYPE
If a cursor returns multiple fields the output is fetched
into a variable declared using the %ROWTYPE data type
The %ROWTYPE can also assume the same data types as
the data fields that a cursor returns when it is declared
using the format
<row variable name> <cursor name>%ROWTYPE
HTTP 504 Database Design and Development 76 November 2, 2014
Fetching Data Into an Explicit CURSOR
Explicit cursors can be processed using a loop that terminates when all rows
have been processed
Two different structures can be used the LOOP EXIT WHEN and the FOR
loop
Format for the LOOP EXIT WHEN
BEGIN
OPEN <cursor name>
LOOP
FETCH <cursor name> INTO <variables>;
<additional statements
END LOOP;
CLOSE <cursor name>;
END;
Attribute Description