Sei sulla pagina 1di 15

IS8080

Stored Procedures, Triggers, etc.

IS8080 Dr. Mario Guimaraes


Class Will

Start Momentarily

PL/SQL overview
IS8080

Oracles Procedural SQL. Microsofts equivalent: Transact SQL Structure similar to PASCAL and ADA PL/SQL module can be: Stored Procedure, Stored Function or Trigger (or Package or Anonymous Block). Components of a module: variable declaration section, executable section, and the exception handler

PROCEDURES / FUNCTIONS
IS8080

ASU web-site ProcedureA series of statements accepting and/or returning zero or more variables. FunctionA series of statements accepting zero or more variables that returns one value.

Examples
CREATE OR REPLACE PROCEDURE insertPerson ( id IN VARCHAR, DOB IN DATE, fname IN VARCHAR, lname IN VARCHAR) IS counter INTEGER; BEGIN SELECT COUNT(*) INTO counter FROM person p WHERE p.pid = id; IF (counter > 0) THEN -- person with the given pid already exists DBMS_OUTPUT.PUT_LINE('WARNING Inserting person: person with pid ' || id || ' already exists!'); ELSE INSERT INTO person VALUES (id, DOB, fname, lname); DBMS_OUTPUT.PUT_LINE('Person with pid ' || id || ' is inserted.'); END IF; CREATE FUNCTION find_circle_area (p_radius IN circle.radius%TYPE ) RETURN NUMBER IS My_area number := 0; Pi constant number := 3.14159265358; BEGIN My_area := (p_radius*p_radius)* Pi; Return my_area; END;

Calling Stored Procedures


IS8080

CREATE OR REPLACE PROCEDURE insertPerson ( id IN VARCHAR, DOB IN DATE, fname IN VARCHAR, lname IN VARCHAR) IS counter INTEGER; BEGIN SELECT COUNT(*) INTO counter FROM person p WHERE p.pid = id; IF (counter > 0) THEN -- person with the given pid already exists DBMS_OUTPUT.PUT_LINE('WARNING Inserting person: person with pid ' || id || ' already exists!'); ELSE INSERT INTO person VALUES (id, DOB, fname, lname); DBMS_OUTPUT.PUT_LINE('Person with pid ' || id || ' is inserted.'); END IF;

--------------------------------------------------------------------------------------------------------------CREATE OR REPLACE PROCEDURE insertFaculty (pid IN VARCHAR, DOB IN DATE, fname IN VARCHAR, lname IN VARCHAR, rank IN VARCHAR, dept IN VARCHAR) IS BEGIN insertPerson(pid, DOB, fname, lname); insert into facultyEDB values(pid, rank, dept); DBMS_OUTPUT.PUT_LINE('Faculty with pid ' || pid || ' is inserted.'); END insertFaculty; -----------------------------------------------------------------------------------------------------------------

exec insertFaculty('121-11-1111', '21-OCT-1961', 'Susan', 'Urban', 'Emeritus', 'CSE'); -- from sql prompt or stmt.executeUpdate ("Insert into customers (cid, cname, city, discnt) values " + "('C011','IBM','Atlanta',30)"); -- from within a Java program

Triggers
IS8080

A series of PL/SQL statements attached to a database table that execute whenever a triggering event (select, update, insert, delete) occurs.
Unlike stored procedures and functions, they not explicitly called, but they are activated when a triggering event occurs. Obs.: main purpose is to implement the complex integrity constraints that cant be done with the CREATE TABLE or ALTER TABLE command.

Example of a Trigger
IS8080

CREATE or REPLACE TRIGGER IncreaseDiscount AFTER INSERT on Orders FOR EACH ROW BEGIN UPDATE Customers SET discnt = discnt+.1 WHERE Customers.cid=:new.cid; END; /

Name of Trigger: IncreaseDiscount Triggering Event: After insert on orders Trigger Body or Trigger code that is fired: UPDATE customers :new and :old represent reserved words for Oracles PL/SQL. In this example, :new represents the cid of the new row in the orders table that was just inserted.

Storing Triggers
IS8080

Where are Triggers, Stored Procedures and Functions Stored ?


What is the difference between (a Trigger) and a (Stored Prodedure/Function) ? What is the difference between a Function and a Procedure ?

Trigger w/Insert
IS8080

CREATE OR REPLACE TRIGGER faculty_before_insert_row BEFORE INSERT ON facultyEDB FOR EACH ROW DECLARE counter INTEGER; OverLap EXCEPTION; -- declare exception BEGIN SELECT COUNT(1) INTO counter FROM student s WHERE s.pid = :new.pid; IF (counter > 0) THEN RAISE Overlap; END IF; EXCEPTION WHEN Overlap THEN RAISE_APPLICATION_ERROR(-20001, 'ERROR: the person already exists as a student in the database! Insert aborted.'); END; 1) Name of Trigger ? 2) Triggering Event ? 3) What is the Trigger Doing ?

Trigger w/ Update
IS8080

CREATE OR REPLACE TRIGGER faculty_after_update_row AFTER UPDATE ON facultyEDB FOR EACH ROW

BEGIN
IF UPDATING ('dept') AND :old.dept <> :new.dept THEN UPDATE department SET chair = NULL WHERE chair = :old.pid; END IF;

END; /

What is this trigger doing ?

Trigger w/ Delete
IS8080

CREATE OR REPLACE TRIGGER faculty_after_delete_row AFTER DELETE ON facultyEDB FOR EACH ROW BEGIN DELETE FROM person WHERE pid = :old.pid; END; /

Trigger Fails
IS8080

When we do an INSERT that fires a Trigger and the insert is sucessful, but the trigger fails (for example, the trigger is trying to access a row that is locked by another user), will the INSERT be undone (ROLLBACK) ?
See trigger on coffee web-site for answer.

Anonymous Block
IS8080

Example: DECLARE percent_id agents.percent%TYPE; BEGIN SELECT percent INTO percent_id FROM agents WHERE aid = 'a02'; IF percent_id > 0 THEN INSERT INTO agents (aid, aname, city) VALUES ('a07', 'John', 'Corpus'); END IF; END; / What does agents.percent%TYPE mean ?

Unlike Triggers and Stored Procedures/Functions, They are not stored inside the database, but They are stored as a .sql file in an individual users area and executed from the sql prompt.

JDBC and SQLJ


IS8080

JDBC Java Database Connectivity A predefined set of classes and methods for accessing SQL databases SQLJ SQL for Java The Oracle pre-compiler for JAVA. It takes simple Oracle calls and translates them into JAVA code prior to compilation with javac

Using JDBC
IS8080

Steps to use a JDBC Download java from sun web-site and place it in proper directory Download jdbc driver for oracle from oracle website Change connection string (host computer, port number, database instance)

PL/SQL versus JAVA


IS8080

Potrebbero piacerti anche