School of Computing and Management Sciences © Sheffield Hallam University SQL is non-procedural –designed to be relatively approachable to non- programmers PL/SQL is a procedural extension to SQL –many Oracle products are themselves written in pl/sql –but it IS proprietary What is PL/SQL?
School of Computing and Management Sciences © Sheffield Hallam University Standard DML commands SQL functions SQL transaction control creation and control of “cursors” flow control - IF, WHEN, LOOP…. Error Handling But not DDL – dynamic sql What can be done with PL/SQL?
School of Computing and Management Sciences © Sheffield Hallam University Anonymous Block Stored Procedure or Function Application Procedure or Function Database Trigger Table trigger Application Trigger Packages Where does PL/SQL live?
School of Computing and Management Sciences © Sheffield Hallam University Modular Blocks may be nested, but each block has the following general structure: –Declaration –Executable (Begin….End) –Exception Handling PL/SQL Structure
School of Computing and Management Sciences © Sheffield Hallam University Variable declaration within DECLARE block: –part_no NUMBER(4); Two approaches to assigning values: –either: part_no := 999 ; tax := price * tax_rate ; –OR SELECT sal * 0.10 INTO bonus FROM emp WHERE empno = emp_id ; PL/SQL Variables and Constants
School of Computing and Management Sciences © Sheffield Hallam University Declaring a constant is like declaring a variable except that you must add the keyword CONSTANT and immediately assign a value to the constant. credit_limit CONSTANT REAL := ; PL/SQL Variables and Constants
School of Computing and Management Sciences © Sheffield Hallam University Cursors –pointer to tables or views –rows can be FETCHED into variables and then manipulated –They need Opening and Closing Attributes –%Type, %ROWCOUNT, %NOTFOUND Important PL/SQL concepts
Block Types Anonymous [DECLARE] BEGIN --statements [EXCEPTION] END; [DECLARE] BEGIN --statements [EXCEPTION] END; Procedure PROCEDURE name IS | AS BEGIN --statements [EXCEPTION] END; PROCEDURE name IS | AS BEGIN --statements [EXCEPTION] END; Function FUNCTION name RETURN datatype IS BEGIN --statements RETURN value; [EXCEPTION] END; FUNCTION name RETURN datatype IS BEGIN --statements RETURN value; [EXCEPTION] END;
Syntax for Creating Procedures Syntax CREATE [OR REPLACE] PROCEDURE procedure_name (parameter1 [mode1] datatype1, parameter2 [mode2] datatype2,...) IS | AS BEGIN... END; Values of variables in the parameter list are populated by the calling environment OR by the procedure OR both, depending on the mode. Mode can be IN (default), OUT or IN OUT
Triggers A trigger is a PL/SQL block that executes implicitly whenever a particular event takes place Application INSERT INTO emp....; EMPNOENAMEJOBSAL KING BLAKE SMITH SCOTT PRESIDENT MANAGER CLERK ANALYST CHECK_SAL Trigger
Creating Triggers Trigger timing −For table:BEFORE, AFTER Triggering event:INSERT, UPDATE or DELETE Trigger Type:Row or Statement When clause: Restricting condition Trigger body: PL/SQL block
Creating Statement Triggers Syntax CREATE [OR REPLACE] TRIGGER trigger_name timing event1 [OR event2 OR event3] ON table_name BEGIN trigger_body END; timingBEFORE or AFTER eventINSERT, UPDATE or DELETE trigger bodyPL/SQL block (DECLARE – BEGIN – END)
Creating Statement Triggers Example CREATE OR REPLACE TRIGGER secure_emp BEFORE INSERT ON emp BEGIN IF (TO_CHAR(sysdate, 'DY') IN ('SAT', 'SUN') ) OR (TO_CHAR(sysdate, 'HH24') NOT BETWEEN '08' AND '18') THEN RAISE_APPLIATION_ERROR (-20500, 'You may only insert into EMP during normal hours.'); END IF; END; /
CREATE OR REPLACE TRIGGER derive_commission_pct BEFORE INSERT OR UPDATE OF sal ON emp FOR EACH ROW BEGIN IF NOT (:new.job IN ('MANAGER', 'PRESIDENT') ) THEN RAISE_APPLICATION_ERROR (-20202, 'Employee cannot earn this amount'); END IF; END; / Creating Row Triggers Example Within a ROW Trigger only :new.columnreferences the new value of the column :old.columnreferences the old value of the column (The : prefix is NOT required in the WHEN restricting condition)
School of Computing and Management Sciences © Sheffield Hallam University PL/SQL building a Procedure