Download as rtf, pdf, or txt
Download as rtf, pdf, or txt
You are on page 1of 9

declare

vfno number;
begin
vfno:=100;
declare
vsno number;
vtol number;
begin
vsno:=200;
vtol:=vfno+vsno;
display('the total '||vfno||' and '||vsno||' is '||vtol);
end;
display(vfno);
diplay(vsno);
end;

Note:

In this case 'vsno' doesn't display because varible used outside the second block part.
The varible which is declare in second block cannot use first block part area ,but the
variable which is declare in first part can be used in second part.
The second block part ends when we declare end part.
declare
m number:=100;
begin
declare
n number:=200;
tol number;
m number;
begin
m:=300;
n:=400;
tol:=m+n;
display('the tol of m,n is '||tol);
display('the n value is '||n);
display('the inner block m is '||m);
end;
display('the outer block m is '||m);
end;

Note:

If we declare 'm' in both in inner block and outer block and if we assign a value in second block
then the change occurs in the second block 'm' variable only.
To use first block 'm' variable in second block if the second having same 'm' vairable the use
labelled block to use the 'm' in second block.
EX:

Labeled block:
<<block1>>declare
m number:=100;
begin
<<block2>>declare
n number:=200;
tol number;
m number;
begin
block1.m:=300;
m:=500;
n:=400;
tol:=m+n;
display('the tol of m,n is '||tol);
display('the n value is '||n);
display('the inner block m is '||m);
end;
display('the outer block m is '||m);
end;
-----------------------------------------------declare
m number:=250;
begin
display('The value of outter m '||m);
declare
m number:=550;
begin
display('the value of outter m '||m);
end;
display('the value of outter m '||m);
end;
---------------------------------------------------------declare
vename varchar2(10);
vsal number;
vdeptno number;
vgross number;
begin
select ename,sal,deptno into vename,vsal,vdeptno
from emp
where empno=&empno;
vgross:=vsal+vsal*0.45+vsal*0.35-vsal*0.15;
display('The emp details are '||vename||' '||vdeptno||' '||vsal||' '||vgross);
end;
--------------------------------------------------------------

declare
vename varchar2(10);
vsal number;
vdeptno number;
vnetsal number;
begin
select ename,sal,deptno,sal+nvl(comm,0) into vename,vsal,vdeptno,vnetsal
from emp
where empno=&empno;
display('The emp details are '||vename||' '||vdeptno||' '||vsal||' '||vnetsal);
end;
>alter table emp modify ename varchar2(20);
O/p: no problem in execution;
>insert into emp (empno,ename) values (1001,siva rama krishna)
o/p:no problem since the size of column is changed
but the pl/sql cannot execute since in program we used varchar(10) ,
so there wiil error so we use variable attributes.
---------------------------------------------------------------------------------VARIABLE ATTRIBUTES
COLUMN TYPE
declare
vename emp.ename%type;
begin
select ename into vename
from emp
where empno=&empno;
display('the employee name is '||vename);
end;
---------------------------------------------------------------------------------declare
i emp%rowtype;
vnetsal number;
begin
select ename,job,deptno,sal,sal+nvl(comm,0) into i.ename,i.job,i.deptno,i.sal,vnetsal
from emp
where empno=&empno;
display('the employee details are '||i.ename||' '||i.job||' '||i.deptno||' '||i.sal||' '||vnetsal);
end;
-----------------------------------------------------------------------------------ROW TYPE :
declare
i emp%rowtype;
begin
i.empno:=&empno;

select ename,sal into i.ename,i.sal


from emp where empno=i.empno;
if i.sal<1000
then
i.sal:=i.sal+100;
update emp set sal=i.sal
where empno=i.empno;
display(i.ename||' '||i.sal);
insert into upd_tab_table values(i.empno,i.ename,i.sal);
end if;
end;
-----------------------------------------------------------------------------------------------------------declare
i student%rowtype;
vtol number(5);
vavg number(5);
vlow number(5);
vhigh number(5);
begin select name,m1,m2,m3,m4,m5 into i.name,i.m1,i.m2,i.m3,i.m4,i.m5
from student
where sid=&sid;
vtol:=i.m1+i.m2+i.m3+i.m4+i.m5;
vavg:=(i.m1+i.m2+i.m3+i.m4+i.m5)/5;
vlow:=least(i.m1+i.m2+i.m3+i.m4+i.m5);
vhigh:=greatest(i.m1+i.m2+i.m3+i.m4+i.m5);
display('ename totalmarks avgeragemarks lowestmarks highestmarks');
display(rpad(i.name,10)||rpad(vtol,10)||rpad(vavg,10)||rpad(vlow,10)||rpad(vhigh,10));
end;
____________________________________________________________________________
CONTROL STRUCTURES
Control statement
declare
vyear number(4):=&year;
begin
if (mod(vyear,400)=0) or (mod(vyear,4)=0 and mod(vyear,100)<>0) then
display('it is a leap year');
else
display('it is not a leap year');
end if;
end;
declare
vempno emp.empno%type:=&empno;
vsal emp.sal%type;
begin
select sal into vsal from emp
where empno=vempno;
if vsal between 0 and 1000 then
vsal:=vsal+100;

elsif vsal between 1001 and 2000 then


vsal:=vsal+200;
else
vsal:=vsal+300;
end if;
update emp set sal=vsal
where empno=vempno;
display('the '||vempno||' salary is '||vsal);
end;
declare
vaccno kcb_acc_tab.accno%type:=upper('&accno');
vttype kcb_tran_tab.ttype%type:=upper('&ttpye');
vamt kcb_tran_tab.amt%type:=&amt;
vcbal kcb_acc_tab.bal%type;
vacctype kcb_acc_tab.acctype%type;
begin
select acctype,bal into vacctype,vcbal from kcb_acc_tab
where accno=vaccno;
if vttype='W' then
vcbal:=vcbal-vamt;
elsif vttype='D' then
vcbal:=vcbal+vamt;
if vacctype='S' AND VCBAL<5000 then
raise_application_error(-20345,'the bal is too low so no transcation');
elsif vacctype='C' and vcbal<10000 then
raise_application_error(-20345,'the bal is too low so no transcation');
end if;
end if;
update kcb_acc_tab set bal=vcbal
where accno=vaccno;
insert into kcb_tran_tab values(nvl((select max(tid) from
kcb_tran_tab),100)+1,vaccno,vttype,sysdate,vcbal);
end;
declare
cursor c(vempno emp.empno%type,vjob emp.job%type)
is
select ename,job,deptno from emp
where empno=vempno and job=vjob;
i c%rowtype;
begin
open c(&empno,'&job');
fetch c into i;
display(rpad(i.ename,8)||' '||rpad(i.job,8)||' '||i.deptno);
end;
DECLARE
CURSOR C IS
SELECT DEPTNO FROM DEPT;
CURSOR S(VDEPTNO EMP.DEPTNO%TYPE) IS
SELECT EMPNO,ENAME,SAL BASIC,SAL*0.45 HRA,SAL*0.35 DA,SAL*0.15 PF,DEPTNO

FROM EMP
WHERE DEPTNO=VDEPTNO;
I C%ROWTYPE;
J S%ROWTYPE;
VGROSS NUMBER(7,2);
BEGIN
OPEN C;
LOOP
FETCH C INTO I;
EXIT WHEN C%NOTFOUND;
OPEN S(I.DEPTNO);
LOOP
FETCH S INTO J;
EXIT WHEN S%NOTFOUND;
VGROSS:=J.BASIC+J.HRA+J.DA-J.PF;
INSERT INTO EMP_REPORT
VALUES(J.EMPNO,J.ENAME,J.BASIC,J.HRA,J.DA,J.PF,VGROSS,J.DEPTNO);
END LOOP;
IF S%ROWCOUNT>0 THEN
INSERT INTO EMP_REPORT (ECODE) VALUES (NULL);
END IF;
CLOSE S;
END LOOP;
END;
declare
type erec is record
(veno number(4),
vename emp.ename%type,
basic emp.sal%type,
i dept%rowtype,
vgross number(16,2));
e erec;
begin
e.veno:=&empno;
select ename,sal,emp.deptno,dname into
e.vename,e.basic,e.i.deptno,e.i.dname
from emp,dept
where emp.deptno=dept.deptno and empno=e.veno;
e.vgross:=e.basic+e.basic*0.25+e.basic*0.35-e.basic*0.12;
display(e.veno||' '||e.vename||' '||e.basic||' '||e.i.deptno||' '||e.i.dname);
end;
declare
type name is table of
varchar(50)
index by binary_integer;
n name;
begin
n(0):='siva';
n(1):='rama';
n(2):='krsihna';

display('the full name is'||n(0)||' '||n(1)||' '||n(2));


end;
declare
type eno is table of
emp.empno%type index by binary_integer;
type name is table of
emp.ename%type index by binary_integer;
type pays is table of
emp.sal%type index by binary_integer;
e eno;
n name;
p pays;
ctl number:=1;
begin
for i in (select empno,ename,sal from emp)
loop
e(ctl):=i.empno;
n(ctl):=i.ename;
p(ctl):=i.sal;
ctl:=ctl+1;
end loop;
for k in 1..e.count
loop
if p(k) between 0 and 1000 then
p(k):=p(k)+100;
elsif p(k) between 1001 and 2000 then
p(k):=p(k)+300;
else
p(k):=p(k)+300;
end if;
update emp set sal=p(k)
where empno=e(k);
insert into trace values (n(k),p(k),sysdate);
display(n(k)||' '||p(k));
end loop;
end;

declare
type pf_info is record
(pfno number(4),
amount number(14,2));
type emp_rec is record
(eid number(4),
name varchar2(20),

basic number(12,2),
pf pf_info);
type etab is table of emp_rec
index by binary_integer;
ctr number(3):=1;
e etab;
begin
for i in (select empno,ename,sal basic,sal*12 pamt from emp where sal>2000)
loop
e(ctr).eid:=i.empno;
e(ctr).name:=i.ename;
e(ctr).basic:=i.basic;
e(ctr).pf.pfno:=i.empno+5;
e(ctr).pf.amount:=i.pamt;
ctr:=ctr+1;
end loop;
display('employee details are:');
for j in 1..e.count
loop
display(e(j).eid||' '||e(j).name||' '||e(j).basic||' '||e(j).pf.pfno||' '||e(j).pf.amount);
end loop;
end;
declare
type name is table of
emp.ename%type index by binary_integer;
type pays is table of
emp.sal%type index by binary_integer;
n name;
p pays;
begin
select ename,sal bulk collect into n,p
from emp;
for i in 1..n.count
loop
display(rpad(n(i),9)||' '||p(i));
end loop;
end;
create or replace procedure proc_bonus
as
cursor cb is
select empno,ename,job,sal+nvl(comm,0) netsal
from emp;
i cb%rowtype;
bonus number;
begin
open cb;
loop
fetch cb into i;
exit when cb%notfound;
if i.job='CLERK'

then bonus:=i.netsal*0.35;
elsif i.job='SALESMAN'
THEN bonus:=i.netsal*0.25;
ELSE
bonus:=i.netsal*0.15;
END IF;
END LOOP;
close cb;
END proc_bonus;
create or replace function feo(n in number,f out number)
return boolean
is
fact number:=1;
begin
if n=0 or n=1 then
f:=1;
else
for i in reverse 1..n
loop
fact:=fact*i;
end loop;
end if;
if mod(n,2)=0 then
return (true);
else
return (false);
end if;
end feo;

You might also like