Beruflich Dokumente
Kultur Dokumente
1 Introduction to PL/SQL
4 SQL in PL/SQL
5 Control Structures
8 Cursors
9 Packages
10 Exceptions
11 Triggers
…
…. Query
Invoking
PL/SQL SQL DATABASE
PL/SQL
PROCEDURE Data
….
….
………………..SERVER…………..……..
Begin
[ Exception]
….
End;
Anonymous
Block Packages
PL/SQL
BLOCK
Stored
Procedure/ Triggers
Programs
Procedures Functions
Begin
select job
into v_designation Data Retrieval from Table
into Variable
from emp
where ename='SCOTT';
if (v_designation = 'MANAGER') then
update emp set sal=sal*1.5 where ename='SCOTT';
else
update emp set sal=sal*1.05 where ename='SCOTT';
End if;
End;
BEGIN
DBMS_OUTPUT.PUT_LINE(A); -- Print A
…..
A is 100 or 200?
END; -- Inner Block ends
DBMS_OUTPUT.PUT_LINE(A);
END;
PL/SQL Block 2
begin
dbms_output.put_line (:x); --prints 20
end;
IF (CNT>10) THEN
DBMS_OUTPUT.PUT_LINE (‘Exceeded limit 10’);
EXIT;
END IF;
• Example
drop procedure retrieve;
• select ename
from emp
where sal>ret_maxsal(20);
Example
– DROP FUNCTION ret_maxsal
Will remove the stored function ret_maxsal.
Also remove the related data dictionary entries and contents.
DATA DICTIONARY
Declare
DESC EMP
V_SAL EMP.SAL%TYPE;
…..
SAL NUMBER(7,2)
….
NUMBER(7,2)
DECLARE
TYPE my_data is RECORD
(ename emp.ename%TYPE,
dname dept.dname%TYPE);
my_emp my_data;
BEGIN
SELECT ename,dname
INTO my_emp
FROM emp,dept
WHERE emp.deptno=dept.deptno AND empno=&ENO;
dbms_output.put_line(my_emp.ename ||my_emp.dname);
END;
START
No into in select
DECLARE CURSOR
Declare
Cursor my_cur is select ename,sal from emp;
OPEN CURSOR
open my_cur; Brings the active set into
the cursor area
NP
IS
NO
EMPT
Y
YES
CLOSE CURSOR
close my_cur;
STOP
declare
cursor dep_cur is select * from emp where deptno=10 order by sal;
begin
dbms_output.put_line('-------------------------------');
dbms_output.put_line ('Sl.No. Salary details');
dbms_output.put_line('-------------------------------');
FOR dep_rec IN dep_cur
LOOP
dbms_output.put_line (dep_cur%rowcount||' '||dep_rec.ename
||'''s salary is '||dep_rec.sal);
END LOOP;
dbms_output.put_line('-------------------------------');
end;
Procedures
– Cursors
– And all PL/SQL allowed Constructs
Example
CREATE OR REPLACE PACKAGE bonus_pack
IS
v_bonus NUMBER:=0;
PROCEDURE retrieve_bonus(p_eno NUMBER);
END bonus_pack;
SQL:>EXECUTE bonus_pack.v_bonus:=1000;
SQL:>EXECUTE bonus_pack.retrieve_bonus(7788);
BEGIN
bonus_pack.v_bonus:=200;
dbms_output.put_line(bonus_pack.v_bonus);
….
bonus_pack.retrieve_bonus(7369);
END;
Example
CREATE OR REPLACE PACKAGE dept_pack
IS
v_dept_cnt NUMBER:=0;
PROCEDURE print_details;
END dept_pack;
COUNT(*)
----------
5
PRAGMA EXCEPTION_INIT
– Associates name “unique constraint” with the error code
-0001
Exception got declared, handled and not invoked
159 © 2009 Wipro Ltd – Internal & Restricted
RAISE APPLICATION ERROR
• Used to print user defined error message
….
RAISE_APPLICATION_ERROR(-20010,'Check Constraint Violation');
…
ERROR at line 1:
ORA-20010: Check Constraint Violation