PL/SQL Procedural Language based on Günther Stürner: 7 - A User s and Developer s Guide Michael R. Ault: 7.0 Administration & Management 8.16.. 10 R2 manuals Feuerstein et al: PL/SQL Language Application Processing PL/SQL Block Database Server Database PL/SQL block declare begin exception end; PL/SQL engine SQL engine DB engine Stored PL/SQL procedures / functions 1
PL/SQL in Client/Server /* calling a stored PL/SQL program */ exec sql execute begin abc (:x, :y, :z); end; end exec; sql sql proc appl. devel. tool PL/SQL engine Sqlplus Applications... Forms... Database Server PL/SQL engine SQL processor & DB engine abc Config file SET.. SQL*Plus Sqlplus commands (see SQL*Plus manuals) sqlplus Spool File declare x. y z begin abc (x, y, z); end; @script.sql PL/SQL engine Native SQL PL/SQL language (see PL/SQL manuals) Database Server (instance) SQL processor & DB engine (see SQL Reference manual) PL/SQL abc 2
isql*plus ks SQL*Plus User s Guide and Reference isql*plus Help 3
Types of PL/SQL programs 4GL procedures or functions client side triggers of Forms, Menu, Reports Anonymous PL/SQL blocks 3GL embedded SQL SQL*Plus scripts (host or client) Stored PL/SQL programs Created procedures, functions, packages stored in database in compiled form Database triggers (remote operations secured by 2PC) Declarative part Variable and constant declarations SQL datatypes, with NOT NULL constraint and initial value PL/SQL datatypes, arrays and records Cursor definitions Declare c1 (<parameters>) select <parameters>; User-defined exceptions myexception exception; max_op_cursors exception; user defined error names overriding error codes Pragma definitions (compiler directives) pragma exception_init( max_op_cursors, -1000) RESTRICT_REFERENCES SERIALLY_REUSABLE AUTONOMOUS_TRANSACTION 4
Identifiers, variables Identifiers: Names of PL/SQL objects: constants, variables, labels, exceptions, functions, procedures, cursors, record types,... Consists of letter, digits 0..9, dollar sign ($), underscore (_), number sign (#) Cannot include whitespaces (space, tab, carriage return) Must start with a letter A..Z Not case-sensitive (except quoted identifiers) Up to 30 characters long Declaring variables: <variable name> <datatype> [CONSTANT] [NOT NULL] [ {:= DEFAULT} <initial value> ] ; Delimiters In addition to typical SQL delimiters ** exponentiation concatenation := assignment = equal to <>!= not equal to ^= ~= << >> label : host variable % attribute indicator / anchoring %TYPE {<cursor> <table>}%rowtype <cursor>%<attribute> @ remote database indicator.. Range indicator => named parameter assignment <parameter> => <value> 5
Predefined PL/SQL Datatypes Number Types NUMBER (SQL, fixed point 38 digits or floating point in range [1E-130.. 1.0E126) ) BINARY_FLOAT, BINARY_DOUBLE (IEEE compatible) BINARY_INTEGER and subtypes (PL/SQL only) NATURAL, NATURALN, POSITIVE, POSITIVEN, SIGNTYPE ISO compatible subtypes INT[EGER], SMALLINT, DEC[IMAL] (will map into SQL NUMBER) FLOAT, REAL, DOUBLE PRECISION (will map to SQL FLOAT) Character Types and Large Objects CHAR[ACTER], NCHAR[ACTER], VARCHAR[2], NVARCHAR[2] LONG, LONG RAW, RAW, ROWID, UROWID BLOB, CLOB, NCLOB, XMLType (SQL) Boolean Types BOOLEAN (PL/SQL only) Date, Time and Interval Types DATE, TIMESTAMP [WITH [LOCAL] TIMEZONE] (SQL) INTERVAL YEAR TO MONTH, INTERVAL DAY TO SECOND.. datatypes, records, collections, arrays Data type copy myvar1 employee.emp_name%type; myvar2 myvar1%type; Row type copy of columns and data types for a record emp_rec employee%rowtype; emp_rec.salary := 1000; Single dimensional arrays type string_arr is table of varchar2(100) indexed by binary_integer; textline string_arr ; Associative Arrays ( index-by tables) Nested Tables Varrays variable-size arrays 6
Examples:.. Records TYPE emp_rec_type IS RECORD ( no smallint, name varchar2(50), birthdate date ) ; emp_rec emp_rec_type; emp_rec.no := 100; emp_rec.name := 'Jones Tom'; emp_rec.birthdate := to_date ('1980-10-10', 'YYYY-MM-DD'); Executable part Assignments variable := value expression SELECT column[s] INTO variable[s] FROM FETCH cursor INTO variable[s] Conditional processing IF THEN ELSE END IF Unconditional branching GOTO label <<label>> Loop structures LOOP END LOOP EXIT WHEN condition FOR END LOOP FORALL i IN 1..imax <statement> WHILE END LOOP Cursor loops Cursor control EXECUTE IMMEDIATE SQL-statement-string Raising exceptions 7
Native Dynamic SQL EXECUTE IMMEDIATE <SQL statement string> [ INTO { <define variable list> <record> <object variable> } ] [ USING [ IN OUT IN OUT ] <bind argument list> ] ; Note: bind parameters allowed only just like in embedded SQL. For object names etc use concatenation of VARCHAR2 values. Example: DECLARE deptname VARCHAR2(14) := 'Development'; location VARCHAR2(13) := 'Helsinki'; BEGIN EXECUTE IMMEDIATE 'insert into dept (deptno,dname,loc) ' 'values (:1, :2, :3) ' USING 50, deptname, location; END; Cursor processing Cursor status attributes: %FOUND %NOTFOUND %ROWCOUNT %ISOPEN Examples: if c1%found then if c1%isopen then DECLARE cursor C1 is select * from T order by ; c1_rec C1%rowtype; BEGIN open C1; loop fetch C1 into c1_rec; exit when C1%notfound; end loop; total_count := C1%rowcount; close C1; 8
Cursor FOR loop FOR <record> IN { <cursor name> (SELECT ) } LOOP <executable statements> END LOOP ; <record> is defined automatically as <cursor name>%rowtype FOR loop does automatically cursor open, fetch, and close operations Example: FOR emp_rec IN SELECT * FROM emp LOOP IF emp_rec.dept = 'D123' THEN salary_raise(emp_rec.emp_id, 15) ; END IF ; END LOOP ; Raising Exceptions Exception can be raised by PL/SQL engine RAISE statement RAISE <exception name> calling RAISE_APPLICATION_ERROR Exception handler can process and then re-raise the current exception by RAISE ; 9
Exception Handling BEGIN EXCEPTION When <exception name> then <statements>... When OTHERS then err_code := sqlcode; err_text := sqlerrm; insert into program_errors (error_code, error_text, error_time) values (err_code, err_text, sysdate); END; After exception processing the PL/SQL block is ended and control returns to the calling module or next statement after the END in case of local exceptions of a BEGIN-END block. Exceptions with PL/SQL mnemonics Exception name: for error code: Source: Stürner, 7 Cursor_already_open ORA-06511 dup_val_on_index ORA-00001 invalid_cursor ORA-01001 invalid_number ORA-01722 login_defined ORA-01017 no_data_found ORA-01403 not_logged_on ORA-01012 program_error ORA-06501 storage_error ORA-06500 timeout_on_resource ORA-00051 too_many_rows ORA-01422 transaction_backed_out ORA-00061 value_error ORA-06502 zero_divide ORA-01476 for the updated list with corresponding sqlcode values and explantions see PL/SQL User s Guide and Reference 10
Autonomous Transactions PROCEDURE main IS BEGIN INSERT ; My_at_proc ; UPDATE ; COMMIT; END; PROCEDURE My_at_proc IS PRAGMA AUTONOMOUS_TRANSACTION; BEGIN INSERT ; UPDATE ; DELET ; COMMIT; END; Changes in main transaction are not visible in the AT-transaction, but changes committed in the AT-transaction are visible in the calling transaction (even if it started after the main transaction) and other transactions. Developing a stand-alone procedure Create or Replace function/procedure p... Show errors function/procedure p PL/SQL compiler SQL processor Data Dictionary Error$ -tables Database object definitions Source / code Compiled code Status Dependencies 11
Create Function SQL > CREATE OR REPLACE FUNCTION <fname> (p1 <datatype>, p2<datatype>, ) RETURN <datatype> IS <PL/SQL block> The PL/SQL code is compiled by PL/SQL compiler parsed by SQL-processor and stored in the data dictionary Any errors are stored in the data dictionary table Error$ to accessed by views USER_ERRORS, ALL_ERRORS or DBA_ERRORS or SQL> SHOW ERRORS FUNCTION <fname> An example function SQL> CREATE OR REPLACE FUNCTION DayOfWeek (indate IN DATE) 2 RETURN SMALLINT 3 IS BEGIN 4 RETURN TO_NUMBER(TO_CHAR(indate,'D')); 5 END; 6 / Function created. SQL> SELECT DayOfWeek(SYSDATE) FROM DUAL; DAYOFWEEK(SYSDATE) ------------------ 3 Note: DUAL is a virtual single line table which is typically used for reporting current system data 12
Create Procedure SQL: CREATE OR REPLACE PROCEDURE <pname> (p1 IN <datatype>, p2 OUT <datatype>, ) IS <PL/SQL block> For full syntax see the SQL Reference manual SQL> SHOW ERRORS or SQL> SHOW ERRORS PROCEDURE <pname> or SQL> select * from user_errors where name = <pname> ; Create Trigger CREATE OR REPLACE <trigger name> { BEFORE AFTER INSTEAD OF} { {<trigger event> ON { NESTED TABLE <nested_table_column> OF <view> <table> <view>} [ <referencing clause> ] [FOR EACH ROW [WHEN <trigger condition> ] ] } <trigger event> ON DATABASE } <trigger body> ; INSERT UPDATE DELETE CREATE ALTER DROP LOGON LOGOF START SHUTDOWN SERVERERROR 13
Triggered RowVersioning CREATE TABLE VersionTest ( id INT, -- primary key s VARCHAR2(20), -- data columns.. rv NUMBER DEFAULT 0, -- reserved for triggered RowVersioning CONSTRAINT PK_VersionTest PRIMARY KEY (id) ) ; CREATE OR REPLACE TRIGGER TRG_VersionTest BEFORE UPDATE ON VersionTest FOR EACH ROW BEGIN :NEW.rv := :OLD.rv + 1; END; / PL/SQL Packages Encapsulation of modules Administration of procedures Managing access privileges Declaring global variables and constants available in the same session Hiding private procedures,.. Overloading 14
Specification part declaring the visible objects Creating the code and hidden objects Initialization block to be executed on the first call by a new user Creating PL/SQL Package Create or Replace Package pack1 as procedure p1 (a in number, ); function f1 (af in number) return number; var_1 ; my_ex exception; cursor c1; end pack1; Create or Replace Package Body pack1 as procedure p1 (a in number, ); <code> function f1 (af in number) return number; <code> procedure p_private ( ); <code> begin var_1 := <value>;... end pack1; Builtin Packages - Feuerstein 96 - PL/SQL packages DBMS_ALERT Notifications of database events DBMS_DDL Interface to some SQL DDL statements DBMS_JOB Automatic submitting of scheduled database tasks DBMS_LOCK Interface for user defined locks using OLM DBMS_MAIL Interface to Mail ( <= 9) DBMS_OUTPUT Messaging from PL/SQL programs DBMS_PIPE ShMem messaging between sessions DBMS_SESSION Interface to session level commands DBMS_SQL Dynamic SQL from PL/SQL DBMS_TRANSACTION Interface for transaction options DBMS_UTILITY Miscellaneous PL/SQL routines DBMS_JAVA see Database Java Developer's Guide. 15
.. Builtin Packages DBMS_STREAMS DBMS_XDB DBMS_XML... DBMS_RLS... HTF functions generating HTML tags HTP procedures generating HTML tags OWA_... Web API, cookies etc SDO_... Spatial Data Objects? UTL_FILE reading and writing op-sys files UTL_HTTP communicating with Web servers UTL_SMTP communicating with mail servers Debugging with DBMS_OUTPUT dbms_output.enable (<buffersize>); dbms_output. put (var); put_line(var); new_line; Database Cache Pipe dbms_output.disable; SQL> set serveroutput on SQL> execute <proc> ; <lines displayed on the terminal> dbms_output. get_line (line,status); get_lines (lines, numlines, status); 16
DBMS_PIPE session A Database Cache session B dbms_pipe. reset_buffer pack_message(ov); send_message(f); Pipe dbms_pipe. receive_message(ov) unpack:message(ov) DBMS_MAIL transaction Select update.... dbms_mail.send.. Commit / Rollback After successful commit 17
DBMS_LOCK Package for explicit locking (by integer values) user defined locks between 1-1073741823 Allocate_unique - name associated for integers 1073741824- Request - request a lock with a given mode Convert - convert the mode of a lock Sleep (secs) - suspend the session for a given period of time Release - releasing the lock A better Sleep ( ) CONNECT System@ORCL -- Source: 10g R2 PL/SQL User's Guide and Reference CREATE PROCEDURE java_sleep (milli_seconds IN NUMBER) AS LANGUAGE JAVA NAME 'java.lang.thread.sleep(long)'; / CREATE PROCEDURE sleep (seconds IN NUMBER) IS BEGIN java_sleep (seconds * 1000); END; / CREATE PUBLIC SYNONYM java_sleep FOR system.java_sleep; CREATE PUBLIC SYNONYM sleep FOR system.sleep; / GRANT EXECUTE ON java_sleep TO PUBLIC; GRANT EXECUTE ON sleep TO PUBLIC; / CALL sleep(10); 18