PL/SQL
Oracle's Procedural SQL
Usually PL/SQL programs are written within other applications (such as forms or reports).
- Variable Declaration
variable_name data_type;
- Reference Variable declarations inherit their data type from a referenced database field.
variable_name table_name.field_name%TYPE;
- Scalar Data Types
Database Scalar data types
- VARCHAR2
- CHAR
- DATE
- LONG
- NUMBER
Non-Database Scalar data types
- Integers
- Decimal Numbers
- Boolean
- PL/SQL Program Structure
DECLARE
Variable Declarations
BEGIN
Program Statements
EXCEPTION
Error-handling Statements
END;
- Comments
Multiline comments are delimited with /* ... */
Single line comments begin with two hyphens --
- Operators
+ - * / and **
=, <> or !=, >, <, >=, <=
assignment is :=
string concatenation is ||
- Activate Memory Buffer
SQL> SET SERVEROUPUT ON SIZE buffer_size; (typical = 4000)
This command must be executed to activate the internal buffer that displays program output.
- Output Data
DBMS_OUTPUT.PUT_LINE('output_string');
You can only write one line at a time and can display only character data.
- / will execute PL/SQL in SQL*PLUS
- Data Type Conversion Functions
TO_DATE(date_string, date_format_string);
TO_NUMBER(number_string);
TO_CHAR(date_type, date/time_format_string);
- Character String Functions
RTRIM(string)
LENGTH(string)
UPPER(string), LOWER(string)
start_location = INSTR(string_to_search, string_searched_for);
sub_string = SUBSTR(string, location, number_of_chars);
- IF-THEN-ELSE-END IF
IF condition THEN
statements
ELSE
statements
END IF;
- LOOPS
Loop
statements
IF condition
EXIT;
END IF;
statements
END LOOP;
LOOP
statements
EXIT WHEN condition;
END LOOP;
WHILE condition;
LOOP
statements
END LOOP;
FOR counter
IN start_val .. end_val
LOOP
statements
END LOOP;
- Cursors
A cursor is a pointer that references the context area which contains command information such as the number of rows processed and the active set (the set of data rows returned by the query).
Cursor Attributes
- %NOTFOUND - True when no rows left to fetch
- %FOUND - True when rows left to fetch
- %ROWCOUNT - Number of rows a cursor has fetched so far
- %ISOPEN - True if cursor is open
Implicit Cursors are created automatically for each INSERT, UPDATE, DELETE and SELECT command.
The command may return only one record to use an implicit cursor.
SELECT field_names
INTO declared_variable_names
FROM table_names
WHERE join_condition
AND record_selection_condition_for_exactly_one_record;
Explicit cursors must be declared and can be used with queries that return any number of records.
DECLARE
variable declarations
CURSOR cursor_name IS
Select_statement;
BEGIN
OPEN cursor_name;
LOOP
FETCH cursor_name INTO
variable_name(s);
EXIT WHEN cursor_name%NOTFOUND;
END LOOP
CLOSE cursor_name;
END;
/
DECLARE
variable declarations
CURSOR cursor_name IS
Select_statement;
%ROWTYPE declaration
BEGIN
FOR variable_name(s) in cursor_name LOOP
statements
END LOOP;
END;
/
DECLARE
variable declarations
CURSOR cursor_name IS
SELECT field_names
FROM table_name
WHERE record_selection_condition
FOR UPDATE;
row_variable cursor_name%ROWTYPE
BEGIN
FOR variable_name(s) in cursor_name LOOP
UPDATE table_name
SET field_name = value
WHERE CURRENT OF cursor_name;
END LOOP;
END;
/
NOTE: WHERE CURRENT OF is a reference to the current row.
- Tables are indexed arrays of data from a field in a table.
First define the appropriate user-defined data subtype.
Then declare the table itself.
TYPE user-defined_data_subtype_name IS TABLE OF item_data_type
INDEX BY BINARY_INTEGER;
table_name user-defined_data_subtype_name;
Insert data into a table with an ordinary array assignment.
table_variable_name(index) := value;
Example:
DECLARE
TYPE state_code_table IS TABLE OF VARCHAR2(2);
state_codes state_code_table;
state_codes(1) := 'AK';
PL/SQL Table Attributes
- COUNT - number of rows in the table
- DELETE(range) - deletes rows
- EXISTS(row_key) - returns true or false
- FIRST - returns key of 1st item
- LAST - returns key of last item
- NEXT(row_key) - returns next key
- PRIOR(row_key) - returns previous key
- Table of Records
A table of records stores one or more entire records from a table in memory for fast access; especially useful for lookups involving small tables.
Define a table of records using the following syntax.
TYPE table_data_type_name
IS TABLE OF database_table_name%ROWTYPE
INDEX BY BINARY_INTEGER;
To assign a value to a field of a record in a table of records use the following syntax.
table_name(key_value).database_field_name := field_value;
- Error Handling in PL/SQL Programs.
Code to handle run-time errors in a PL/SQL program is located in the EXCEPTION section of a PL/SQL program.
PL/SQL Program Structure
DECLARE
Variable Declarations
BEGIN
Program Statements
EXCEPTION
Error-handling Statements
END;
Top of this page
Home page 
