Table of Contents [Hide/Show]
Basic Block Structure Variables Declaration Syntax Scalar Data Types Assignment Retrieving Data into Variables Bind Variables Output Control Structures PL/SQL Records PL/SQL Tables Declaration Syntax Referencing Syntax PL/SQL Table Methods Exception Handling Syntax Predefined Exceptions Functions Used Within Exception Handling User-Defined Errors PRAGMA EXCEPTION_INIT Statement Raising Errors See Also
VARIABLE g_Hello VARCHAR2(46)
DECLARE v_variable VARCHAR2(5);
BEGIN SELECT column_name INTO v_variable FROM table_name;
 :g_Hello := 'Hello, world!'
EXCEPTION WHEN exception_name THEN . . .
END;
identifier [CONSTANT] datatype [NOT NULL] [:= | DEFAULT expr];
identifier
CONSTANT
datatype
RECORD
TABLE
VARRAY
LOB
NOT NULL
expr
VARCHAR2(maxlen)
NUMBER [(precision, scale)]
DATE
CHAR [(maxlen)]
LONG
LONG RAW
BOOLEAN
TRUE
FALSE
NULL
BINARY_INTEGER
PLS_INTEGER
NUMBER
column_name%TYPE
column_name
identifier := expr
SELECT
FETCH
SELECT ColumnNameList INTO { variable_name [, variable_name] . . . | record_name } FROM table WHERE condition
NO_DATA_FOUND
TOO_MANY_ROWS
VARIABLE identifier datatype
SQL> VARIABLE g_n NUMBER. . .PRINT g_n
:g_Hello := 'Hello, world!'
SET SERVEROUTPUT ON DBMS_OUTPUT.PUT_LINE('Monthly salary = ' || TO_CHAR(v_sal));
IF ''condition'' THEN ''statements''; [ELSIF ''condition'' THEN // NOTE SPELLING OF KEYWORD !!! ''statements'';] [ELSE ''statements'';] END IF;
LOOP statement1; . . . EXIT [WHEN condition]; END LOOP;
FOR ''counter'' in [REVERSE] ''lower_bound''..''upper_bound'' LOOP ''statements''; END LOOP
REVERSE
lower_bound
counter
WHILE condition LOOP statements; END LOOP
TYPE type_name IS RECORD (field_declaration [, field_declaration] . . .); identifier type_name;
field_declaration
field_name {field_type | variable%TYPE | table.column%TYPE | table%ROWTYPE} [[NOT NULL] {:= | DEFAULT | expr]
. . . TYPE emp_record_type IS RECORD (ename VARCHAR2(10), // these column names should appear job VARCHAR2(9), // in the same order as in the database sal NUMBER(7,2)); emp_record emp_record_type; . . .
DECLARE identifier reference%ROWTYPE;
reference
TYPE type_name IS TABLE OF { column_type | variable%TYPE | table.column%TYPE } [NOT NULL] [INDEX BY BINARY_INTEGER]; identifier type_name;
TYPE ename_table_type IS TABLE OF emp.ename%TYPE INDEX BY BINARY_INTEGER; ename_table ename_table_type;
pl/sql_table_name(primary_key_value)
primary_key_value
ename_table(3) . . .
table_name.method_name[ (parameters) ]
EXISTS(n)
n
COUNT
FIRST
LAST
PRIOR(n)
NEXT(n)
EXTEND(n, i)
EXTEND
EXTEND(n)
i
TRIM
TRIM(n)
DELETE
DELETE(n)
DELETE(m, n)
m..n
EXCEPTION WHEN exception1 [OR exception2 . . . ] THEN statements; [WHEN exception3 [OR exception3 . . .] THEN statements;] [WHEN OTHERS THEN statements;]
exception
INVALID_CURSOR
ZERO_DIVIDE
DUP_VAL_ON_INDEX
SQLCODE
SQLERRM
DECLARE e_emps_remaining EXCEPTION; PRAGMA EXCEPTION_INIT (e_emps_remaining, -2292) . . . EXCEPTION WHEN e_emps_remaining THEN . . . END;
RAISE_APPLICATION_ERROR
PRAGMA EXCEPTION_INIT
WHEN OTHERS
PRAGMA EXCEPTION_INIT(exception_name, error_number)
PROCEDURE xxx() IS PRAGMA EXCEPTION_INIT(BAD_CUSTOMER_ACCOUNT, -20001) BEGIN RAISE_APPLICATION_ERROR(BAD_CUSTOMER_ACCOUNT, 'Customer account' || cust_account_in || ' is invalid'); EXCEPTION WHEN BAD_CUSTOMER_ACCOUNT DBMS_OUTPUT.PUT_LINE(SQLERRM); // log the error message RAISE; // re-raise the exception for VB END;
Err.Number
Err.Message
OUT
ROLLBACK
raise_application_error(error_number, message [, use_stack ] );
error_number
message
use_stack
RAISE_APPLICATION_ERROR(-20001, 'Customer account' || cust_account_in || ' is invalid')'
ScrewTurn Wiki version 3.0.1.400. Some of the icons created by FamFamFam. Except where noted, all contents Copyright © 1999-2024, Patrick Jasinski.