By Bill Pribyl, Steven Feuerstein, Chip Dawes
Cover | Table of Contents
Constant width is used for code examples.[] 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.[] 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, carriage return
|
|
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 or collection.
|
|
Reference
|
Pointers to values.
|
|
LOB
|
Variables containing Large OBject (LOB) locators.
|
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;
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
DBMS_OUTPUT.PUT_LINE('Beginning Forward');
FOR counter IN 1 .. 4
LOOP
DBMS_OUTPUT.PUT_LINE('counter='||counter);
END LOOP;
DBMS_OUTPUT.PUT_LINE('Beginning REVERSE');
FOR counter IN REVERSE 1 .. 4
LOOP
DBMS_OUTPUT.PUT_LINE('counter='||counter);
END LOOP;
END;
FOR record_index IN [cursor_name | (SELECT statement)] LOOP executable_statement(s) END LOOP;
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 IS
SELECT * FROM company;DECLARE exception_name EXCEPTION;
|
Error
|
Named Exception
|
|---|---|
|
ORA-00001
|
DUP_VAL_ON_INDEX
|
|
ORA-00051
|
TIMEOUT_ON_RESOURCE
|
|
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
|
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 empname_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
CREATE [OR REPLACE] PROCEDURE name [ (parameter [,parameter]) ] [AUTHID CURRENT_USER | DEFINER ] -- Oracle8i [DETERMINISTIC] -- Oracle8i IS | AS declaration_section BEGIN executable_section [EXCEPTION exception_section] END [name];
apply_discount(new_company_id, 0.15) --15% discount
CREATE [OR REPLACE] FUNCTION name [ (parameter [,parameter]) ] RETURN return_datatype [AUTHID CURRENT_USER | DEFINER ] -- Oracle8i [DETERMINISTIC] -- Oracle8i [PARALLEL_ENABLE] -- Oracle8i IS | AS [declaration_section] BEGIN executable_section [EXCEPTION exception_section] END [name];
CREATE [OR REPLACE] TRIGGER trigger_name
BEFORE | AFTER | INSTEAD OF trigger_event
ON
[ NESTED TABLE nested_table_column OF view ]
| table_or_view_reference | DATABASE
[referencing_clause]
[FOR EACH ROW [WHEN trigger_condition]]
trigger_body;
|
Trigger Event
|
Description
|
|---|---|
|
INSERT
|
Fires whenever a row is added to the table_reference.
|
|
UPDATE
|
Fires whenever an UPDATE changes the table_reference. UPDATE triggers can additionally specify an OF clause to restrict firing to updates OF certain columns. See the following examples.
|
|
DELETE
|
Fires whenever a row is deleted from the table_reference. Does not fire on TRUNCATE of the table.
|
|
CREATE (Oracle8i)
|
Fires whenever a CREATE statement adds a new object to the database. In this context, objects are things like tables or packages (found in ALL_OBJECTS). Can apply to a single schema or the entire database. |
CREATE [OR REPLACE] PACKAGE package_name
[ AUTHID CURRENT_USER | DEFINER ] -- Oracle8i
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]
-- 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] -- Oracle8i
AS OBJECT (
attribute_name datatype,...,
[MEMBER | STATIC PROCEDURE | FUNCTION
program_spec],
[ORDER | MAP MEMBER FUNCTION
comparison_function_spec],
[PRAGMA RESTRICT_REFERENCES(program_name,
purities)]
);
|
Collection Type
| |||
|---|---|---|---|
|
Characteristic
|
Index-by Table
|
Nested Table
|
VARRAY
|
|
Dimensionality
|
Single
|
Single
|
Single
|
|
Usable in SQL?
|
No
|
Yes
| |
CREATE [OR REPLACE] LIBRARY library_name IS | AS 'absolute_path_and_file';
DROP LIBRARY library_name;
Return to Oracle PL/SQL Language Pocket Reference