Professional Documents
Culture Documents
PLSQL Syntax Structure
PLSQL Syntax Structure
Procedure:
PROCEDURE name (p_parm1 IN|INOUT datatype, … )
/* declarations */
IS
BEGIN
/* executable code */
EXCEPTION
/* error handling */
END name;
/
Function:
FUNCTION name name (p_parm1 IN|INOUT datatype, … )
RETURN datatype
/* declarations */
IS
BEGIN
/* executable code */
EXCEPTION
/* error handling */
END name;
/
Anonymous Block:
DECLARE
/* declarations */
IS
BEGIN
/* executable code */
EXCEPTION
:/* error handling */
END name;
/
CURSOR cursor_name
[(parameter_name datatype, ...)]
IS select_statement;
Cursor Attributes
cursor%ROWCOUNT - int - number of rows affected by last SQL statement
cursor%FOUND - bool - TRUE if >1 row returned
cursor%NOTFOUND - bool - TRUE if 0 rows returned
cursor%ISOPEN - bool - TRUE if cursor still open
DECLARE Section
Declare variables and constants in a PL/SQL declare block.
name [CONSTANT] datatype [NOT NULL] [:= | DEFAULT expr]
key
ﺳﺎﻣﺢ ﺑﻜﺎر/م
Options
'field_declaration' is defined as:
field_name {datatype | variable%TYPE | table.column%TYPE | table%ROWTYPE}[
[NOT NULL] {:= | DEFAULT} expr ]
Example:
-- Declare a variable based on SQL*Plus Bind variable
v_amount NUMBER(6,2) := &p_foo
(column_type |
variable%TYPE |
table.column%TYPE
[NOT NULL]
INDEX BY BINARY INTEGER;
Example:
IF NOT mycursor%ISOPEN THEN
OPEN mycursor FOR select_statement;
CLOSE mycursor;
END IF;
EXCEPTION Syntax
EXCEPTION
WHEN exception1 [OR exception2...]] THEN
...
[WHEN exception3 [OR exception4...] THEN
...]
[WHEN OTHERS THEN
...]
Where exception is the exception_name e.g. NO_DATA_FOUND, etc. Note that only
one handler is processed before leaving the block.
If a sub block does not have a handler for a particular error it will propagate to the
enclosing block - where it can be caught by more general exception handlers.
The variables must match (both in number and positionally) the columns listed in the
cursor definition.
A PL/SQL FOR Loop will implicitly declare a counter, or cursor variable. Remember
that open, fetch, close functions are all implicit in the FOR-LOOP statement.
ﺳﺎﻣﺢ ﺑﻜﺎر/م
NOTE:
null AND null = null
null OR null = null
true AND null = null
true OR null = true
false AND null = false
false OR null = null
NOT NULL = NULL
PL/SQL Operators
Comparison Operators
+ - * / @ ; = <> != || <= >=
NOT IS NULL
LIKE
BETWEEN
IN
AND
OR
Comments
-- comment
/* comment */
<< Begin label - end label >>
Assignment operator
ﺳﺎﻣﺢ ﺑﻜﺎر/م
:=
Delimiters
Item separator .
Character string delimiter '
Quoted String delimiter "
Bind variable indicator :
Attribute indicator %
Statement terminator ;
Functions
All SQL functions that return a single row can be used in a plsql procedural statement.
Note that the Group and DECODE functions are not supported.
Examples
v_myDate := TO_DATE('01-OCT-2001',DD-MON-YYYY)