28
PL/SQL ISYS 464

PL/SQL ISYS 464. PL/SQL Block-Structured Variable declaration section Executable section Exception-handling section

Embed Size (px)

Citation preview

Page 1: PL/SQL ISYS 464. PL/SQL Block-Structured Variable declaration section Executable section Exception-handling section

PL/SQL

ISYS 464

Page 2: PL/SQL ISYS 464. PL/SQL Block-Structured Variable declaration section Executable section Exception-handling section

PL/SQL Block-Structured

• Variable declaration section

• Executable section

• Exception-handling section

Page 3: PL/SQL ISYS 464. PL/SQL Block-Structured Variable declaration section Executable section Exception-handling section

Variable Declaration

• Variable name: begin with a letter and up to 30 characters.

• Data types:– Character: CHAR(n), VARCHAR2(n)– Number: INTEGER, INT, SMALLINT– DECIMAL(i,j), NUMBER(i,j)– Boolean– Date

Page 4: PL/SQL ISYS 464. PL/SQL Block-Structured Variable declaration section Executable section Exception-handling section

Examples

• DECLAREEid CHAR(3);

Ename VARCHAR2(25);

HireDate DATE DEFAULT SYSDATE;

Salary NUMBER(7,2)

TaxRate NUMBER(4,2) :=5.25

Page 5: PL/SQL ISYS 464. PL/SQL Block-Structured Variable declaration section Executable section Exception-handling section

%TYPE

• Use a column’s data type as the data type for the variable:– CustomerName Customer.Cname%TYPE;

Page 6: PL/SQL ISYS 464. PL/SQL Block-Structured Variable declaration section Executable section Exception-handling section

Executable Section

• BEGIN– Statements

• END;

• /– “/ “ signals the end of program.

Page 7: PL/SQL ISYS 464. PL/SQL Block-Structured Variable declaration section Executable section Exception-handling section

Assignment Statement

• Tax := Salary * TaxRate

• Arithemetic operators:– +, -, *, /, **

• Concatenation:– ||

• FullName := (FirstName || ‘ ‘ || LastName);

Page 8: PL/SQL ISYS 464. PL/SQL Block-Structured Variable declaration section Executable section Exception-handling section

Substitution Variables

• &variableName– Ex. &Salary

• On screen you will see:– Enter value for Salary:

• Select * from Customer• Where CID=‘&CID’;

• Ex1: Get input from screen:– Salary := &Salary;

Page 9: PL/SQL ISYS 464. PL/SQL Block-Structured Variable declaration section Executable section Exception-handling section

SELECT … INTO

• SELECT columns separated by commas• INTO variables separated by commas• FROM tablename• WHERE condition;• Ex1:

– SELECT cid, cname INTO custID, customername– FROM customer– WHERE cid = ‘c01’;

• Ex2: Using substituion variable– SELECT cid, cname INTO custID, customername– FROM customer– WHERE cid = ‘&cid’;

Page 10: PL/SQL ISYS 464. PL/SQL Block-Structured Variable declaration section Executable section Exception-handling section

Decisions

• IF condition THEN– Statements /* each statement ends with ; */

• END IF;

• IF … THEN … ELSE …END IF;

• IF … THEN ELSIF ……ELSE … END IF;

Page 11: PL/SQL ISYS 464. PL/SQL Block-Structured Variable declaration section Executable section Exception-handling section

Loop

• WHILE condition LOOP– Statements

• END LOOP;

• FOR i IN 1 .. 10 LOOP– Statements

• END LOOP

Page 12: PL/SQL ISYS 464. PL/SQL Block-Structured Variable declaration section Executable section Exception-handling section

Printing

• SET SERVEROUTPUT ON;• DBMS_OUTPUT.PUT_LINE(string to print);

– DBMS_OUTPUT is a build in package, PUT_LINE is a procedure in that package.

• Ex.– Tax:=Salary * TaxRate;

– DBMS_OUTPUT.PUT_LINE(‘Your tax is: ‘ || Tax);

Page 13: PL/SQL ISYS 464. PL/SQL Block-Structured Variable declaration section Executable section Exception-handling section

Caltax.sqldeclare taxRate number(3,2); eid emp.empid%type; empSalary emp.salary%type; tax number(7,2);begin taxRate :=&taxRate; select salary into empSalary from emp where empid = '&eid';tax := empSalary * taxRate;dbms_output.put_line('Tax is : ' || tax);end;/

Page 14: PL/SQL ISYS 464. PL/SQL Block-Structured Variable declaration section Executable section Exception-handling section

Exceptions

• Exceptions are errors.

• Predefined exceptions:– No_Data_Found: Select statement returns no row.

– Too_Many_Rows: Single-Row Select statement returns more than one row.

– Value_Error: Data conversion errors.

Page 15: PL/SQL ISYS 464. PL/SQL Block-Structured Variable declaration section Executable section Exception-handling section

Handling Exceptions

• Exception– WHEN exceptionname THEN

– Statements

– WHEN OTHERS THEN– Statements

• Ex: PLSQLException.SQL

DeclareCustName Customer.Cname%Type;

begin select Cname into Custname from customer where cid='&CID';

dbms_output.put_line('Customer name is : ' || Custname);exception when no_data_found then dbms_output.put_line('Customer record not exist');end;/

Page 16: PL/SQL ISYS 464. PL/SQL Block-Structured Variable declaration section Executable section Exception-handling section

Creating Stored Procedure

• A procedure is a named program block:CREATE [OR REPLACE] PROCEDURE procedurename

IS[Variable declarations]BEGIN

Statements

END;/

Note: To run a procedure: EXECUTE procedurename

Page 17: PL/SQL ISYS 464. PL/SQL Block-Structured Variable declaration section Executable section Exception-handling section

create or replace procedure addorder (

ordid orders.oid%TYPE,

custID orders.cid%TYPE,

salesID orders.sid%TYPE,

orddate orders.odate%TYPE)

is

tempid customers.cid%TYPE;

begin

select cid into tempid from customers where cid=custid;

insert into orders values(ordid,custid,salesid,orddate);

exception

when no_data_found then

dbms_output.put_line('Customer record not exist');

end;

/

Note: To run a stored procedure: execute addorder('O8','C1','S3','05-NOV-07');

Page 18: PL/SQL ISYS 464. PL/SQL Block-Structured Variable declaration section Executable section Exception-handling section

Functions

CREATE [OR REPLACE] FUNCTION functionname

IS[Variable declarations]BEGIN

StatementsRETURN expression;

END;/

Page 19: PL/SQL ISYS 464. PL/SQL Block-Structured Variable declaration section Executable section Exception-handling section

create or replace function emptax(sal emp.salary%type)

return number is

tax number(6,2);

begin

if sal < 2000.0 then

tax:=sal*0.1;

elsif sal <4000.0 then

tax:=sal*0.2;

else

tax:=sal*0.3;

end if;

return tax;

end;

/

Page 20: PL/SQL ISYS 464. PL/SQL Block-Structured Variable declaration section Executable section Exception-handling section

Using the User-defined Function with SQL

• SELECT empid, ename, emptax(salary)

• FROM emp;

Page 21: PL/SQL ISYS 464. PL/SQL Block-Structured Variable declaration section Executable section Exception-handling section

Cursors

• A cursor is a pointer to a set of records returned by a SQL statement. It enables you to take a set of records and deal with it on a row-by-row basis.

Page 22: PL/SQL ISYS 464. PL/SQL Block-Structured Variable declaration section Executable section Exception-handling section

Defining and Using Cursors

• Declare cursor:– CURSOR cursorname IS SQL statement

• Ex: cursor maleemp is

select empid,ename,salary from emp where sex='M';

Open cursor:OPEN maleemp;

Fetch data into variables:FETCH maleemp into eid,empname,sal;

Use %FOUND to test if record exist

CLOSE cursor

Page 23: PL/SQL ISYS 464. PL/SQL Block-Structured Variable declaration section Executable section Exception-handling section

declare

eid emp.empid%type;

empname emp.ename%type;

sal emp.salary%type;

cursor maleemp is

select empid,ename,salary from emp where sex='M';

begin

open maleemp;

fetch maleemp into eid,empname,sal;

while maleemp%found loop

dbms_output.put_line(eid || empname || sal);

fetch maleemp into eid,empname,sal;

end loop;

close maleemp;

end;

/ Note: To run a cursor: SQL> Start C:\Empcursor1.sql

Page 24: PL/SQL ISYS 464. PL/SQL Block-Structured Variable declaration section Executable section Exception-handling section

Cursor with Argumentsdeclare

eid emp.empid%type;

empname emp.ename%type;

sal emp.salary%type;

cursor gender(s emp.sex%type) is

select empid,ename,salary from emp where sex=s;

begin

open gender('&gender');

fetch gender into eid,empname,sal;

while gender%found loop

dbms_output.put_line(eid || empname || sal);

fetch gender into eid,empname,sal;

end loop;

close gender;

end;

/ Note: ecursor2.sql

Page 25: PL/SQL ISYS 464. PL/SQL Block-Structured Variable declaration section Executable section Exception-handling section

declare eid emp.empid%type; empname emp.ename%type; sal emp.salary%type; gender emp.sex%type; bonus number(6,2);cursor e isselect empid,ename,salary,sex from emp;beginopen e;fetch e into eid,empname,sal,gender;while e%found loop if gender='M' then if sal < 3000 then bonus := sal*.2; else bonus := sal*.1; end if; else if sal < 3000 then bonus:= sal*.25; else bonus:=sal*.15; end if; end if; dbms_output.put_line(eid || empname || sal||gender||bonus); fetch e into eid,empname,sal,gender;end loop;close e;end;/ Note: EmpBonusCursor.SQL

Page 26: PL/SQL ISYS 464. PL/SQL Block-Structured Variable declaration section Executable section Exception-handling section

Triggers

• A trigger is a program stored in the database and is called automatically when a triggering event occurs.

• Update events: – Insert, Delete, Update

• BEFORE Insert|Delete|Update ON tablename

• AFTER Insert|Delete|Update ON tablename

• Accessing new value and old value:– :NEW.fieldname

– :OLD.fieldname

• FOR EACH ROW

Page 27: PL/SQL ISYS 464. PL/SQL Block-Structured Variable declaration section Executable section Exception-handling section

Demo :New and :Oldcreate or replace trigger changesal

after update on emp

for each row

begin

dbms_output.put_line(:old.ename || ' old salary is:' || :old.salary);

dbms_output.put_line('new salary is: ' || :new.salary);

end;

/

Note: EmpSaltrigger.sql

Page 28: PL/SQL ISYS 464. PL/SQL Block-Structured Variable declaration section Executable section Exception-handling section

create or replace trigger adddetail

before insert on orderdetail

for each row

declare

stocks product.onhand%type;

begin

select onhand into stocks

from hproduct where pid=:new.pid;

if stocks < :new.qty then

update product

set onhand=0

where pid = :new.pid;

dbms_output.put_line('not enough stock');

else

update product

set onhand=onhand - :new.qty

where pid=:new.pid;

end if;

end;

/ Note: AddDetailTrigger.sql