Sie sind auf Seite 1von 3

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

Oracle PL/SQL Cheatsheet


Symbols
Statement terminator Attribute indicator (cursor attributes like %ISOPEN and indirect declaration attributes like % Percent sign %ROWTYPE). Also used as multibyte wildcard symbol, as in SQL. _ Single underscore Single- byte wildcard symbol, as in SQL Host variable indicator, such as :block.item in : Colon Oracle Forms ** Double asterisk Exponentiation operator < > and != Not equals" || Double vertical bar Concatenation operator << and >> Label delimiters := Assignment operator => Association operator for positional notation -Double dash: single- line comment indicator Beginning and ending multiline comment block /* and */ delimiters ; Semicolon. IF..THEN..ELSE..ENDIF; LOOP .. IF (condition) THEN EXIT END IF; .. END LOOP: WHILE cond LOOP..END LOOP; FOR var IN n..m LOOP .. END LOOP; EXECUTE function_name; Cursor for.

Cont rol Flow


As usual. Equivalent to if (conition) then break;

while () {}; for thing in range(n,m) {} Function call FUNCTION name (parameter type,..) ..body.. END: Opens cursor, loops across until %NOTFOUND. FOR variables IN cursor LOOP..END LOOP;

Explicit Cursor Handling


Implict cursor named Think of it as a select statement that has a by developer. name. Implict cusror is called IF SQL%NOTFOUND THEN .. SQL DECLARE CURSOR employee_crsr IS Declaring an explicit SELECT empid, salary FROM employee cursor. BEGIN .. OPEN employee_cursor LOOP FETCH employee_cursor INTO my_empid, Executing a cursor my_salary; EXIT WHEN employee_crsr%NOTFOUND; ..do stuff.. ENDLOOP; Obtains next record from cursor.Can fetch into FETCH individual variables (as above) or a RECORD. TYPE t_emp IS RECORD (T_Salary number, t_empid number); Declaring an explicit my_emprec t_emp;
PDFmyURL.com

Dat a Types.
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 DEC, DECIMAL, REAL, DOUBLE- PRECISION, INTEGER, INT, SMALLINT, NATURAL, Non database types. POSITIVE, NUMERIC, BINARY- INTEGER, CHARACTER, VARCHAR, BOOLEAN, TABLE, RECORD Database types NUMBER CHAR(N), VARCHAR2(N) DATE LONG LONG RAW ROWID MLSLABEL

PLSQL Module t ypes


Procedure A non- formal function that can accept paremeters via value or reference. Similar in form to a function. A classical function that returns one value. Usually contains declaration, execution and exception sections. A library, consisting of a specification with function/prototype signatures, and a body with actual code. eg Code attached to a table that fires on certian conditions.

Function

Package Trigger

Module Sect ions or Blocks


DECLARE employee- id employee.empid%TYPE, pi Variable Declaration CONSTANT number := 3.14, ratio REAL,.. BEGIN.. .. BEGIN select * into my_employee Executable Section where employee.emid = 42; END; .. END; Exception Handler. EXCEPTIONS .. END;

Declaring an explicit my_emprec t_emp; cursor using a record. CURSOR employee_crsr IS SELECT empid, salary FROM employee; OPEN employee_cursr; LOOP Executing explicit FETCH emloyee_crsr INTO my_emprec cursror using record. EXIT WHEN employee_crsr%NOTFOUND; IF my_emprec.t_empid .. Declaring parameters to be used at OPEN time. DECLARE .. CURSOR employee_crsr(low_end VARCHAR2, high_end VARCHAR2) IS Cursor Parameters. SELECT empid, salary FROM employee WHERE substr(lastname,1,1) BETWEEN UPPER(low_end) AND UPPER(high_end);

Common except ions


Occurs when you attempt to close a cursor that has not been opened. Occurs when you attempt to open a cursor CURSOR_ALREADY_OPEN the second time DUP_VAL_ON_INDEX Unique or primary key constraint violation More than one row was opbtained by a TOO_MANY_ROWS single row subquery, or another context when Oracle was expecting one row. ZERO_DIVIDE An attempt to divide by z ero. An attempt to FETCH a cursor into an ROWTYPE_MISMATCH incompatible variable type. INVALID_NUMBER An char type was referenced as a number. OTHERS Special catchall exception. INVALID_CURSOR

Package Synt ax
Specification PACKAGE package_name IS [ declarations of variables and types ] [ specifications of cursors ] [ specifications of modules ] END [ package_name ]; PACKAGE BODY package_name IS [ declarations of variables and types ] [ specification and SELECT statement of cursors ] [ specification and body of modules ] [ BEGIN executable statements ] [ EXCEPTION exception handlers ] END [ package_name ];

Pragmas
Tells the compiler to associate a particular EXCEPTION_INIT error number with an identifier you have declared as an exception in your program. Tells the compiler the purity level (freedom RESTRICT_REFERENCES from side effects) of a packaged program. Tells the PL/SQL runtime engine that package- level data should not persist SERIALLY_REUSABLE between references to that data. See Chapter 25, Tuning PL/SQL Applications for more information.

Body

Filename Ext ensions


General SQL*Plus script .sql
PDFmyURL.com

script Testing script Stored procedure Stored function Stored package body Stored package specification %NOTFOUND %ROWCOUNT %FOUND %ISOPEN

.tst .sp .sf spb .sps True if fetch did not return row. Number of rows processed by this cursor Opposite of %NOTFOUND If currently open for processing then true.

Implict cursor at t ribut es.

Transact ion processing


Same Options as SQL COMMIT, ROLLBACK, SAVEPOINT Transaction begins at execution of first Rollbacks go to last COMMIT or SAVE_POINT change of data. DBMS_TRANSACTION A package with functions for transaction control.

Except ion 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'); 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 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);
PDFmyURL.com

User defined.

Pragmas.

Das könnte Ihnen auch gefallen