Vous êtes sur la page 1sur 3

Oracle PL/SQL Cheatsheet

1 of 3

http://www.yagc.ndo.co.uk/cheatsheets/plsql_cheatsheet.html

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License.

Symbols
; Semicolon.
% Percent sign

Statement terminator

Control Flow
IF..THEN..ELSE..ENDIF; As usual.

Attribute indicator (cursor attributes like


LOOP ..
%ISOPEN and indirect declaration
IF (condition) THEN
attributes like %ROWTYPE). Also used as
EXIT END IF;
multibyte wildcard symbol, as in SQL.
.. END LOOP:

Equivalent to if (conition) then br

_ Single underscore Single-byte wildcard symbol, as in SQL WHILE cond LOOP..END


while () {};
LOOP;
Host variable indicator, such as :block.item
: Colon
in Oracle Forms
FOR var IN n..m LOOP
for thing in range(n,m) {}
.. END LOOP;
** Double asterisk Exponentiation operator
EXECUTE
Function call FUNCTION name
< > and !=
Not equals"
function_name;
(parameter type,..) ..body.. END:
|| Double vertical
Concatenation operator
Opens cursor, loops across until
bar
%NOTFOUND.
<< and >>
Label delimiters
Cursor for.
FOR variables IN cursor LOOP..EN
:=
Assignment operator
LOOP;
=>
Association operator for positional notation
Explicit Cursor Handling
-/* and */

Double dash: single-line comment


indicator

Implict cursor
named by
Beginning and ending multiline comment
developer.
block delimiters
Implict cusror is

Data Types.

Database types
NUMBER
CHAR(N),
VARCHAR2(N)
DATE
LONG
LONG RAW
ROWID
MLSLABEL

Definition
Used to store any number
Used for storing text
Oracle system date
Stores large blocks of text
Stores large blocks of binary data
Smaller binary data store
Uesd for row identifier
Security label

called SQL

Think of it as a select statement that


name.
IF SQL%NOTFOUND THEN ..

DECLARE CURSOR employee_crsr IS


Declaring an explicit
SELECT empid, salary FROM employe
cursor.
BEGIN ..
OPEN employee_cursor
LOOP
FETCH employee_cursor INTO my_e
Executing a cursor my_salary;
EXIT WHEN employee_crsr%NOTFOU
..do stuff..
ENDLOOP;

DEC, DECIMAL, REAL, DOUBLEPRECISION, INTEGER, INT, SMALLINT,


Non database types. NATURAL, POSITIVE, NUMERIC, BINARYINTEGER, CHARACTER, VARCHAR,
BOOLEAN, TABLE, RECORD

FETCH

Obtains next record from cursor.Can


into individual variables (as above) o
RECORD.

TYPE t_emp IS RECORD (T_Salary n


t_empid number);
Declaring an explicit
my_emprec t_emp;
A non-formal function that can accept
cursor using a
CURSOR employee_crsr IS
paremeters via value or reference. Similar
record.
SELECT empid, salary
in form to a function.
FROM employee;
A classical function that returns one value.

PLSQL Module types


Procedure

Function

Package

OPEN employee_cursr;
LOOP
Executing explicit
FETCH emloyee_crsr INTO my_empr
A library, consisting of a specification with
cursror using record.
EXIT
WHEN employee_crsr%NOTFOU
function/prototype signatures, and a body
IF my_emprec.t_empid ..
with actual code. eg
Usually contains declaration, execution
and exception sections.

3/13/2012 11:23 PM

Oracle PL/SQL Cheatsheet

2 of 3

http://www.yagc.ndo.co.uk/cheatsheets/plsql_cheatsheet.html

Declaring parameters to be used at O


time.
DECLARE .. CURSOR
Module Sections or Blocks
employee_crsr(low_end VARCHAR2,
Cursor Parameters.
DECLARE
high_end VARCHAR2) IS
employee-id employee.empid%TYPE, pi
SELECT empid, salary FROM employe
Variable Declaration
CONSTANT number := 3.14, ratio REAL,..
WHERE substr(lastname,1,1) BETWE
BEGIN..
UPPER(low_end) AND UPPER(high_e
.. BEGIN select * into my_employee
Common exceptions
Executable Section where employee.emid = 42;
Occurs when you attempt to clo
END; ..
INVALID_CURSOR
cursor that has not been opened
END;
Exception Handler.
Occurs when you attempt to ope
EXCEPTIONS .. END;
CURSOR_ALREADY_OPEN
cursor the second time
Trigger

Code attached to a table that fires on


certian conditions.

Specification

PACKAGE package_name
IS
[ declarations of variables and types ]
[ specifications of cursors ]
[ specifications of modules ]
END [ package_name ];

Package Syntax

DUP_VAL_ON_INDEX

Unique or primary key constrain


violation

TOO_MANY_ROWS

More than one row was opbtain


a single row subquery, or anoth
context when Oracle was expec
one row.

ZERO_DIVIDE
An attempt to divide by zero.
PACKAGE BODY package_name
An attempt to FETCH a cursor in
IS
ROWTYPE_MISMATCH
incompatible variable type.
[ declarations of variables and types ]
[ specification and SELECT statement of
An char type was referenced as
INVALID_NUMBER
cursors ]
number.
[ specification and body of modules ]
OTHERS
Special catchall exception.
[ BEGIN
Pragmas
executable statements ]
Tells the compiler to associate a
[ EXCEPTION
particular error number with an
exception handlers ]
EXCEPTION_INIT
identifier you have declared as a
END [ package_name ];
exception in your program.
Filename Extensions

Body

General SQL*Plus
script

.sql

Testing script

.tst

Stored procedure

.sp

Stored function

.sf

Tells the compiler the purity leve


RESTRICT_REFERENCES (freedom from side effects) of a
packaged program.

SERIALLY_REUSABLE

Stored package body spb


Stored package
specification

.sps

Tells the PL/SQL runtime engine


package-level data should not pe
between references to that data.
Chapter 25, Tuning PL/SQL
Applications for more information

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
Rollbacks go to last COMMIT or
execution of first
SAVE_POINT
change of data.
DBMS_TRANSACTION

A package with functions for transaction


control.

Exception Handling
Predefined

Relates to an oracle error. No need to


invoke. Just catch.
EXCEPTION WHEN NO_DATA_FOUN THEN
DBMS_OUTPUT.PUT_LINE('No data found');

3/13/2012 11:23 PM

Oracle PL/SQL Cheatsheet

3 of 3

http://www.yagc.ndo.co.uk/cheatsheets/plsql_cheatsheet.html

User defined.

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
RAISE my_salary_null;
END IF;
EXCEPTION..
WHEN my_salary_null
THEN DBMS_OUTPUT.PUT_LINE('Salary
column was null for employee');
END

Pragmas.

Associate a predefined error with a


exception handler. eg to have
my_salary_null catch Oracle error -1400
DECLARE
PRAGMA EXCEPTION INIT(my_salary_null,
-1400);

3/13/2012 11:23 PM

Vous aimerez peut-être aussi