PL SQL Coding
PL SQL Coding
K
CURSORS
Explicit Cursor Normal - Select
declare
dno emp.deptno%type:=&Dept_No;
eno emp.empno%type;
ename1 emp.ename%type;
cursor c1 is select empno,ename from emp where deptno=dno;
begin
open c1;
loop
fetch c1 into eno,ename1;
exit when c1%notfound;
dbms_output.put_line(eno);
dbms_output.put_line(ename1);
end loop;
dbms_output.put_line(c1%rowcount);
close c1;
end;
declare
dno emp.deptno%type:=&Dept_No;
eno emp.empno%type;
ename1 emp.ename%type;
esal emp.sal%type;
cursor c1 is select empno,ename,sal from emp where deptno=dno;
begin
open c1;
loop
fetch c1 into eno,ename1,esal;
exit when c1%notfound;
if esal<1200 then
update emp set comm=sal*.6 where deptno=dno and (sal<1200);
elsif esal>1200 and esal<=1500 then
update emp set comm=sal*.4 where deptno=dno and (sal>1200 and sal<=1500);
else
update emp set comm=sal*.5 where deptno=dno and (sal>1500);
end if;
dbms_output.put_line(eno);
dbms_output.put_line(ename1);
end loop;
commit;
dbms_output.put_line(c1%rowcount);
close c1;
end;
declare
dno emp.deptno%type:=&Dept_No;
cursor c1 is select empno,ename,sal from emp where deptno=dno;
r1 c1%rowtype;
begin
for r1 in c1
loop
exit when c1%rowcount>1
if r1.sal<1200 then
update emp set comm=sal*.08 where deptno=dno and (sal<1200);
elsif r1.sal>1200 and r1.sal<=1500 then
update emp set comm=sal*.09 where deptno=dno and (sal>1200 and sal<=1500);
else
update emp set comm=sal*.1 where deptno=dno and (sal>1500);
end if;
end loop;
commit;
exception
when no_data_found then
dbms_output.put_line('Nodata');
declare
dno emp.deptno%type:=&Dept_No;
esal emp.sal%type:=&Esal;
begin
update emp set sal=sal+esal where deptno=dno;
dbms_output.put_line(sql%rowcount);
if sql%notfound then
dbms_output.put_line('No Data');
end if;
end;
Cursor – Parameterized
end;
PL / SQL Coding 3 Amirtharaj.K
declare
a dept.deptno%type:=&DEPT_NO;
b emp.empno%type;
begin
select empno into b from emp where deptno=a;
dbms_output.put_line(b);
exception
when no_data_found then
dbms_output.put_line('No Depts' );
begin
insert into dept(deptno,dname,loc) values(&a,'&b','&c');
dbms_output.put_line(SQL%rowcount);
exception
when dup_val_on_index then
dbms_output.put_line('Duplicate Depts' );
declare
a dept.deptno%type:=&Deptno;
b dept.dname%type:='&Dept_Name';
c exception;
begin
update dept set dname=b where deptno=a;
if sql%notfound then
raise c;
end if;
exception
when c then
dbms_output.put_line(' Depts should be exists' );
begin
insert into dept(deptno,dname,loc) values(&a,'&b','&c');
dbms_output.put_line(SQL%rowcount);
exception
when dup_val_on_index then
dbms_output.put_line('Duplicate Depts' );
dbms_output.put_line(SQLerrm );
dbms_output.put_line(SQLcode);
declare
a dept.deptno%type:=&Deptno;
b dept.dname%type:='&Dept_Name';
c exception;
PRAGMA EXCEPTION_INIT(c,-23);
begin
update dept set dname=b where deptno=a;
if sql%notfound then
raise c;
end if;
exception
when c then
dbms_output.put_line('dept no is missing');
STORED PROCEDURES
Procedure - IN
declare
b number;
a number:=&AA;
begin
p1002(a,b);
dbms_output.put_line(b);
end;
dbms_output.put_line(eno);
dbms_output.put_line(ename1);
end loop;
commit;
dbms_output.put_line(c1%rowcount);
close c1;
dbms_output.put_line(sql%rowcount);
end;
STORED FUNCTION
Function – Body
declare
a number;
begin
a:=f1000(&Empno);
dbms_output.put_line(a);
end;
PACKAGE
exec pc1000.p1100(7900);
-----------------------------------------------------------------------------------------------------------------------
declare
a1 number;
begin
a1:=pc1001.f1100(&empno);
dbms_output.put_line(a1);
exception
when no_data_found then
dbms_output.put_line('No Emp');
end;
-----------------------------------------------------------------------------------------------------------------------
exec pc1002.p1101(7900);
Execute Function
declare
a2 number;
begin
a2:=pc1002.f1101(&empno);
dbms_output.put_line(a2);
exception
when no_data_found then
dbms_output.put_line('Emp Nos None');
end;
declare
a3 number:=&empNo;
a2 number;
begin
a2:=pc1002.f1101(a3);
pc1002.p1101(a3);
dbms_output.put_line(a2);
exception
when no_data_found then
dbms_output.put_line('Emp Nos None');
end;
-----------------------------------------------------------------------------------------------------------------------
PL / SQL Coding 8 Amirtharaj.K
declare
a2 number;
begin
a2:=pc1003.f1103(&empno);
dbms_output.put_line(a2);
exception
when no_data_found then
dbms_output.put_line('Emp Nos None');
end;
-----------------------------------------------------------------------------------------------------------------------
begin
for r1 in c1
loop
exp1:=r1.service;
exit when c1%notfound;
end loop;
return exp1;
end f1104;
end pc1004;
declare
a2 number;
begin
a2:=pc1004.f1104;
dbms_output.put_line(a2);
end;
-----------------------------------------------------------------------------------------------------------------
close c1;
end p1105;
end pc1105;
exec pc1105.p1105;
-----------------------------------------------------------------------------------------------------------------
TRIGGER
Create or replace trigger tr01 after delete on dept for each row
Begin
Insert into dept01 values (:old.deptno,:old.dname,:old.loc);
End;
if d1 in('mon','tue') then
raise_application_error(-20025,'try on another day');
end if;
end;
create or replace trigger tr03 after update on dept for each row
begin
update emp set sal=sal+777 where deptno=:old.deptno;
end;
create or replace trigger tr08 after update of deptno on dept50 for each row
begin
update emp50 set deptno=:new.deptno where deptno=:old.deptno;
end;
create or replace trigger tr09 before delete on dept50 for each row
begin
delete from emp where deptno=:old.deptno;
end;