Oracledba Wdfiles Com Local Files Start PLSQL Cheat Sheet
Oracledba Wdfiles Com Local Files Start PLSQL Cheat Sheet
PL/SQL Cheatsheet
Symbols
; Semicolon. Statement terminator
% Percent Attribute indicator (cursor attributes like %ISOPEN and indirect declaration attributes like
sign %ROWTYPE). Also used as multibyte wildcard symbol, as in SQL.
_ Single
Singlebyte wildcard symbol, as in SQL
underscore
: Colon Host variable indicator, such as :block.item in Oracle Forms
** Double
Exponentiation operator
asterisk
< > and != Not equals"
|| Double
Concatenation operator
vertical bar
<< and >> Label delimiters
:= Assignment operator
=> Association operator for positional notation
Double dash: singleline comment indicator
/* and */ Beginning and ending multiline comment block delimiters
Data Types.
Database types Definition
NUMBER Used to store any number
CHAR(N), Used for storing text
VARCHAR2(N) Oracle system date
DATE Stores large blocks of text
LONG Stores large blocks of binary data
LONG RAW Smaller binary data store
ROWID Uesd for row identifier
MLSLABEL Security label
DEC, DECIMAL, REAL, DOUBLEPRECISION, INTEGER, INT, SMALLINT,
Non database
NATURAL, POSITIVE, NUMERIC, BINARYINTEGER, CHARACTER, VARCHAR,
types.
BOOLEAN, TABLE, RECORD
PLSQL Module types
A nonformal function that can accept paremeters via value or reference. Similar in form to a
Procedure
function.
A classical function that returns one value. Usually contains declaration, execution and exception
Function
sections.
A library, consisting of a specification with function/prototype signatures, and a body with actual
Package
code. eg
Trigger Code attached to a table that fires on certian conditions.
Module Sections or Blocks
DECLARE
Variable Declaration employeeid employee.empid%TYPE, pi CONSTANT number := 3.14, ratio REAL,..
BEGIN..
.. BEGIN select * into my_employee
Executable Section where employee.emid = 42;
END; ..
END;
Exception Handler.
EXCEPTIONS .. END;
Package Syntax
PACKAGE package_name
IS
[ declarations of variables and types ]
Specification
[ specifications of cursors ]
[ specifications of modules ]
END [ package_name ];
PACKAGE BODY package_name
IS
[ declarations of variables and types ]
[ specification and SELECT statement of cursors ]
[ specification and body of modules ]
Body
[ BEGIN
executable statements ]
[ EXCEPTION
exception handlers ]
END [ package_name ];
Filename Extensions
General SQL*Plus script .sql
Testing script .tst
Stored procedure .sp
Stored function .sf
Stored package body spb
Stored package specification .sps
Implict cursor attributes.
%NOTFOUND True if fetch did not return row.
%ROWCOUNT Number of rows processed by this cursor
%FOUND Opposite of %NOTFOUND
%ISOPEN If currently open for processing then true.
Transaction processing
Same Options as SQL COMMIT, ROLLBACK, SAVEPOINT
Transaction begins at execution of first change of data. Rollbacks go to last COMMIT or SAVE_POINT
DBMS_TRANSACTION A package with functions for transaction control.
Exception Handling
Relates to an oracle error. No need to invoke. Just catch.
Predefined EXCEPTION WHEN NO_DATA_FOUN THEN
DBMS_OUTPUT.PUT_LINE('No data found');
Need to be declared, tested and handled in their respective blocks.
DECLARE My_salary_null EXCEPTION; ..
EBGIN..
IF my_emp_record.salary IS NULL THEN
User RAISE my_salary_null;
defined. END IF;
EXCEPTION..
WHEN my_salary_null
THEN DBMS_OUTPUT.PUT_LINE('Salary column was null for employee');
END
Associate a predefined error with a exception handler. eg to have my_salary_null catch Oracle
error 1400
Pragmas.
DECLARE
PRAGMA EXCEPTION INIT(my_salary_null, 1400);
Control Flow
Control Flow
IF..THEN..ELSE..ENDIF; As usual.
LOOP ..
IF (condition) THEN EXIT END IF; Equivalent to if (conition) then break;
.. END LOOP:
WHILE cond LOOP..END LOOP; while () {};
FOR var IN n..m LOOP .. END LOOP; for thing in range(n,m) {}
EXECUTE function_name; Function call FUNCTION name (parameter type,..) ..body.. END:
Opens cursor, loops across until %NOTFOUND.
Cursor for.
FOR variables IN cursor LOOP..END LOOP;
Explicit Cursor Handling
Implict cursor named by
Think of it as a select statement that has a name.
developer.
Implict cusror is called SQL IF SQL%NOTFOUND THEN ..
DECLARE CURSOR employee_crsr IS
Declaring an explicit cursor. SELECT empid, salary FROM employee
BEGIN ..
OPEN employee_cursor
LOOP
FETCH employee_cursor INTO my_empid, my_salary;
Executing a cursor
EXIT WHEN employee_crsr%NOTFOUND;
..do stuff..
ENDLOOP;
Obtains next record from cursor.Can fetch into individual variables (as
FETCH
above) or a RECORD.
TYPE t_emp IS RECORD (T_Salary number, t_empid number);
my_emprec t_emp;
Declaring an explicit cursor using
CURSOR employee_crsr IS
a record.
SELECT empid, salary
FROM employee;
OPEN employee_cursr;
LOOP
Executing explicit cursror using
FETCH emloyee_crsr INTO my_emprec
record.
EXIT WHEN employee_crsr%NOTFOUND;
IF my_emprec.t_empid ..
Declaring parameters to be used at OPEN time.
DECLARE .. CURSOR employee_crsr(low_end VARCHAR2, high_end
VARCHAR2) IS
Cursor Parameters.
SELECT empid, salary FROM employee
WHERE substr(lastname,1,1) BETWEEN UPPER(low_end) AND
UPPER(high_end);
Common exceptions
INVALID_CURSOR Occurs when you attempt to close a cursor that has not been opened.
CURSOR_ALREADY_OPEN Occurs when you attempt to open a cursor the second time
DUP_VAL_ON_INDEX Unique or primary key constraint violation
More than one row was opbtained by a single row subquery, or another context
TOO_MANY_ROWS
when Oracle was expecting one row.
ZERO_DIVIDE An attempt to divide by zero.
ROWTYPE_MISMATCH An attempt to FETCH a cursor into an incompatible variable type.
INVALID_NUMBER An char type was referenced as a number.
OTHERS Special catchall exception.
Pragmas
Pragmas
Tells the compiler to associate a particular error number with an identifier you
EXCEPTION_INIT
have declared as an exception in your program.
Tells the compiler the purity level (freedom from side effects) of a packaged
RESTRICT_REFERENCES
program.
Tells the PL/SQL runtime engine that packagelevel data should not persist
SERIALLY_REUSABLE between references to that data. See Chapter 25, Tuning PL/SQL Applications for
more information.
This work is licensed under a Creative Commons AttributionNonCommercialShareAlike 2.5 License.