Cover | Table of Contents
Constant width is used for code examples, literals, and identifiers.[] enclose optional items in syntax descriptions.{} enclose a list of items in syntax descriptions; you must choose one item from the list.| separates bracketed list items in syntax descriptions.Constant width is used for code examples, literals, and identifiers.[] enclose optional items in syntax descriptions.{} enclose a list of items in syntax descriptions; you must choose one item from the list.| separates bracketed list items in syntax descriptions.Type | Characters |
|---|---|
Letters | A—Z, a—z |
Digits | 0—9 |
Symbols | ˜!@#$%^&*( )_−+=|[ ]{ }:;"'< >,.?/ ^ |
Whitespace | space, tab, newline, carriage return |
$), an underscore (_ ), and a pound sign (#)DECLARE "1 ^abc" VARCHAR2(100); BEGIN IF "1 ^abc" IS NULL THEN ... END;
Type | Description |
|---|---|
Scalar | Variables made up of a single value, such as a number, date, or Boolean. |
Composite | Variables made up of multiple values, such as a record, collection, or instance of a user-defined object type. See the sections "," "," and "." |
Reference | Logical pointers to values or cursors. |
LOB | Variables containing large object (LOB) locators. |
NUMBER(precision, scale)
Declaration | Assigned value | Stored value |
|---|---|---|
NUMBER | 6.02 | 6.02 |
NUMBER(4) | 8675 | 8675 |
NUMBER(4) | 8675309 | Error |
NUMBER(12,5) | 3.14159265 | 3.14159 |
NUMBER(12,−5) | 8675309 | 8700000 |
IF condition THEN executable statement(s) END IF;
IF caller_type = 'VIP' THEN
generate_response('GOLD');
END IF;
IF condition THEN TRUE sequence_of_executable_statement(s) ELSE FALSE/NULL sequence_of_executable_statement(s) END IF;
IF caller_type = 'VIP' THEN
generate_response('GOLD');
ELSE
generate_response('BRONZE');
END IF;
IF condition-1 THEN statements-1 ELSIF condition-N THEN statements-N [ELSE ELSE statements] END IF;
IF caller_type = 'VIP' THEN
generate_response('GOLD');
ELSIF priority_client THEN
generate_response('SILVER');
ELSE
generate_response('BRONZE');
END IF;
CASE region_id
WHEN 'NE' THEN
mgr_name := 'MINER';
WHEN 'SE' THEN
mgr_name := 'KOOI';
ELSE mgr_name := 'LANE';
END CASE;
LOOP
executable_statement(s)
END LOOP;
LOOP
FETCH company_cur INTO company_rec;
EXIT WHEN company_cur%ROWCOUNT > 5 OR
company_cur%NOTFOUND;
process_company(company_cur);
END LOOP;
FOR loop_index IN [REVERSE] lowest_number..highest_number LOOP executable_statement(s) END LOOP;
BEGIN
FOR counter IN 1 .. 4
LOOP
DBMS_OUTPUT.PUT(counter);
END LOOP;
DBMS_OUTPUT.NEW_LINE;
FOR counter IN REVERSE 1 .. 4
LOOP
DBMS_OUTPUT.PUT(counter);
END LOOP;
DBMS_OUTPUT.NEW_LINE;
END;
1234 4321
FOR loop_index IN [cursor_name | (SELECT statement)] LOOP executable_statement(s) END LOOP;
FOR emp_rec IN emp_cur
LOOP
IF emp_rec.title = 'Oracle Programmer'
THEN
give_raise(emp_rec.emp_id,30)
END IF;
END LOOP;
SELECT my_sequence.NEXTVAL INTO my_variable FROM dual;
my_variable := my_sequence.NEXTVAL;
COMMIT [WORK] [comment_text];
ROLLBACK [WORK] [TO [SAVEPOINT] savepoint_name];
CURSOR company_cur IS SELECT company_id FROM company;
CURSOR company_cur (id_in IN NUMBER)
IS
SELECT name FROM company
WHERE company_id = id_in;
CURSOR company_cur (id_in IN NUMBER) RETURN company%ROWTYPE;
OPEN cursor_name [(argument [,argument ...])];
DECLARE
exception_name EXCEPTION;
Error | Named exception |
|---|---|
ORA-00001 | DUP_VAL_ON_INDEX |
ORA-00051 | TIMEOUT_ON_RESOURCE |
ORA-00061 | TRANSACTION_BACKED_ OUT |
ORA-01001 | INVALID_CURSOR |
ORA-01012 | NOT_LOGGED_ON |
ORA-01017 | LOGIN_DENIED |
ORA-01403 | NO_DATA_FOUND |
ORA-01410 | SYS_INVALID_ROWID |
ORA-01422 | TOO_MANY_ROWS |
ORA-01476 | ZERO_DIVIDE |
ORA-01725 | USERENV_COMMMITSCN_ ERROR |
ORA-01722 | INVALID_NUMBER |
ORA-06500 | STORAGE_ERROR |
ORA-06501 | PROGRAM_ERROR |
ORA-06502 | VALUE_ERROR |
ORA-06504 | ROWTYPE_MISMATCH |
ORA-06511 | CURSOR_ALREADY_OPEN |
ORA-06530 | ACCESS_INTO_NULL |
ORA-06531 | COLLECTION_IS_NULL |
ORA-06532 | SUBSCRIPT_OUTSIDE_LIMIT |
ORA-06533 | SUBSCRIPT_BEYOND_COUNT |
ORA-06548 | NO_DATA_NEEDED |
ORA-06592 | CASE_NOT_FOUND |
ORA-30625 | SELF_IS_NULL |
DECLARE
exception_name EXCEPTION;
PRAGMA EXCEPTION_INIT (
DECLARE
-- Declare table-based record for company table.
comp_rec company%ROWTYPE
CURSOR comp_summary_cur IS
SELECT c.company_id,SUM(s.gross_sales) gross
FROM company c ,sales s
WHERE c.company_id = s.company_id;
-- Declare a cursor-based record.
comp_summary_rec comp_summary_cur%ROWTYPE;
DECLARE
TYPE name_rectype IS RECORD(
prefix VARCHAR2(15)
,first_name VARCHAR2(30)
,middle_name VARCHAR2(30)
,sur_name VARCHAR2(30)
,suffix VARCHAR2(10) );
TYPE employee_rectype IS RECORD (
emp_id NUMBER(10) NOT NULL
,mgr_id NUMBER(10)
,dept_no dept.deptno%TYPE
,title VARCHAR2(20)
,name name_rectype
,hire_date DATE := SYSDATE
,fresh_out BOOLEAN );
-- Declare a variable of this type.
new_emp_rec employee_rectype;
BEGIN
record_name.field_name
employee.first_name
BEGIN
insurance_start_date :=
new_emp_rec.hire_date + 30;
new_emp_rec.fresh_out := FALSE;
...
Characteristic | Associative array | Nested table | VARRAY |
|---|---|---|---|
Dimensionality | Single | Single | Single |
Usable in SQL? | No | Yes | Yes |
Usable as a column datatype in a table? | No | Yes; data stored "out of line" (in a separate table) | Yes; data typically stored "inline" (in the same table) |
Uninitialized state | Empty (cannot be NULL); elements are undefined | Atomically null; illegal to reference elements | Atomically null; illegal to reference elements |
Initialization | Automatic, when declared | Via constructor, fetch, assignment | Via constructor, fetch, assignment |
In PL/SQL, elements referenced by | BINARY_INTEGER (−2,147,483,647 .. 2,147,483,647) or character string (VARCHAR2); maximum length of VARCHAR2 is 30, minimum length is 1 | Positive integer between 1 and 2,147,483,647 elements cannot be referenced unless they have been initialized via the constructor or with the EXTEND function | Positive integer between 1 and 2,147,483,647; elements cannot be referenced unless they have been initialized via the constructor or with the EXTEND function |
Built-in function | Description |
|---|---|
ASCIISTR(string) | Converts string in any character set to the ASCII version of string in the database character set. |
CAST(expression AS datatype) | Converts expression to type datatype. (Use as a powerful substitute for TO_DATE.) |
CHARTOROWID(string) | Converts string to a ROWID datatype. |
CONVERT(string, destination_character_set, source_character_set) | Converts string from the source character set to the destination character set. (The default source character set is the database character set.) |
FROM_TZ(timestamp_value, time_zone) | Adds time zone information to a TIMESTAMP value (converting it to a TIMESTAMP WITH TIME ZONE value). |
HEXTORAW(string) | Converts string containing hexadecimal characters to its raw equivalent. |
MULTISET | Maps a database table to a collection. |
NUMTODSINTERVAL(n, interval_unit) | Converts numeric expression n to an INTERVAL DAY TO SECOND literal; interval_unit can be DAY, HOUR, MINUTE, or SECOND. |
NUMTOYMINTERVAL(n, interval_unit) | Converts numeric expression n to an INTERVAL YEAR TO MONTH literal; interval_unit can be YEAR or MONTH. |
RAWTOHEX(raw) | Converts raw value to its hexadecimal equivalent. |
REFTOHEX(ref) | Converts ref expression into a string containing its hexadecimal equivalent. |
ROWIDTOCHAR(rowid) | Converts rowid to a VARCHAR2(18) string equivalent. |
ROWIDTONCHAR(rowid) | Converts rowid to an NVARCHAR2(18) string equivalent. |
TABLE |
CREATE [OR REPLACE] PROCEDURE name [ (parameter [,parameter]) ] [AUTHID { CURRENT_USER | DEFINER } ] { IS | AS } declaration_section BEGIN executable_section [EXCEPTION exception_section] END [name];
apply_discount(new_company_id, 0.15);
CALL apply_discount(new_company_id, 0.15);
EXECUTE apply_discount(new_company_id, 0.15);
BEGIN apply_discount(new_company_id, 0.15); END;
CREATE [OR REPLACE] FUNCTION name [ (parameter [,parameter]) ] RETURN return_datatype [AUTHID { CURRENT_USER | DEFINER } ] [DETERMINISTIC] [PARALLEL_ENABLE [partition_clause]] [PIPELINED [USING implementation_type]] [RESULT_CACHE [RELIES_ON (table_list)]] [AGGREGATE USING implementation_type] { IS | AS } [declaration_section] BEGIN executable_section [EXCEPTION exception_section] END [name];
CREATE [OR REPLACE] TRIGGER trigger_name { BEFORE | AFTER | INSTEAD OF | FOR } trigger_event ON {table_or_view_reference | NESTED TABLE nested_table_column OF view} [REFERENCING [OLD AS old] [NEW AS new] [PARENT AS parent]] [FOR EACH ROW ] [FOLLOWS other_trigger] [DISABLE] [COMPOUND TRIGGER] [WHEN trigger_condition] trigger_body;
CREATE [OR REPLACE] TRIGGER trigger_name { BEFORE | AFTER } trigger_event ON [ DATABASE | schema ] [FOLLOWS other_trigger][DISABLE] [WHEN trigger_condition] trigger_body;
Trigger event | Description |
|---|---|
INSERT | Fires whenever a row is added to the table_ or_view_reference. |
UPDATE | Fires whenever an UPDATE changes the table_or_view_reference. UPDATE triggers can additionally specify an OF clause to restrict firing to updates OF certain columns. |
DELETE | Fires whenever a row is deleted from the table_or_view_reference. Does not fire on a TRUNCATE of the table. |
ALTER | Fires whenever an ALTER statement changes a database object. In this context, objects are things such as tables or packages (found in ALL_OBJECTS). Can apply to a single schema or the entire database. |
ANALYZE | Fires whenever the database collects or deletes statistics or validates the structure of a database object. |
ASSOCIATE STATISTICS | Fires whenever the database associates a statistic type with a database object. |
AUDIT | Fires whenever the database records an audit operation. |
CREATE [OR REPLACE] PACKAGE package_name [ AUTHID { CURRENT_USER | DEFINER } ] { IS | AS } [definitions of public TYPEs ,declarations of public variables, types, and objects ,declarations of exceptions ,pragmas ,declarations of cursors, procedures, and functions ,headers of procedures and functions] END [package_name];
CREATE [OR REPLACE] PACKAGE BODY package_name { IS | AS } [definitions of private TYPEs ,declarations of private variables, types, and objects ,full definitions of cursors ,full definitions of procedures and functions] [BEGIN executable_statements [EXCEPTION exception_handlers]] END [package_name];
[schema_name.][pkg_name.]func_name[@db_link] [parm_list]
GetTimestamp function in the time_pkg example seen earlier in the "" section:-- Capture system events. INSERT INTO v_sys_event (timestamp, event, qty_waits) SELECT time_pkg.GetTimestamp, event, total_waits FROM v$system_event -- Capture system statistics. INSERT INTO v_sys_stat (timestamp, stat#, value) SELECT time_pkg.GetTimestamp, statistic#, value FROM v$sysstat;
CREATE TABLE table_name OF object_type;
CREATE [OR REPLACE] TYPE obj_type_name [ AUTHID { CURRENT_USER | DEFINER } ] { { IS | AS } OBJECT | UNDER parent_type_name } ( attribute_name datatype,..., [ [ [NOT] OVERRIDING ] [ [NOT] FINAL ] [ [NOT] INSTANTIABLE ] method_spec,...,] [PRAGMA RESTRICT_REFERENCES(program_name, purities)] ) [ [NOT] FINAL ] [ [NOT] INSTANTIABLE ];
MEMBER { PROCEDURE | FUNCTION } program_spec
STATIC { PROCEDURE | FUNCTION } program_spec
{ ORDER | MAP } MEMBER FUNCTION comparison_function_spec
[ FINAL ] [ INSTANTIABLE ] CONSTRUCTOR FUNCTION
RETURNING SELF AS RESULT constructor_function_spec
Return to Oracle PL/SQL Language Pocket Reference