PL SQL Programs
PL SQL Programs
P2. WRITE A PROGRAM TO CALCULATE THE SIMPLE INTEREST AND COMPUND INTEREST ,
IF P, N, R ARE GIVEN.
declare
p number(9,2) ;
n number(9,2) ;
r number(9,2) ;
si number(9,2) := 0;
ci number(9,2) := 0;
begin
p := &principal_amount;
n := &no_of_years;
r := &rate_of_interest;
si := p*n*r/100;
ci := p*(1+r/100)**n;
dbms_output.put_line('simple interset =' ||si);
dbms_output.put_line('compound interset =' ||ci);
end;
SQL> /
Enter value for principal_amount: 10000
old 8: p:=&principal_amount;
new 8: p:=10000;
Enter value for no_of_years: 5
old 9: n:=&no_of_years;
new 9: n:=5;
Enter value for rate_of_interest: 10.5
old 10: r:=&rate_of_interest;
new 10: r:=10.5;
simple interset =5250
compound interset =16474.47
PL/SQL procedure successfully completed.
P4. Given 2 sides of a rectangle .Write a program to find out its area is gr
eater than its perimeter or not .
declare
l number;
b number;
ar number;
pr number;
begin
l := &l;
b := &b;
ar := l*b;
pr := 2*(l+b);
if ar > pr then
dbms_output.put_line('the area iS > its perimeter'|| 'area = '||ar||'perimet
er = '||pr);
else
dbms_output.put_line('the area iS < its perimeter'|| ' area = '||ar||' perim
eter = '||pr);
end if;
end;
P5. WRITE A PROGRAM TO INPUT A SINGLE DIGIT NO: CONVERT IT INTO WORDS.
Declare
a number;
t varchar2(10);
begin
a :=&a;
if a=1 then
t := 'one';
elsif a=2 then
t := 'two';
elsif a= 3 then
t := 'three';
elsif a= then
t := 'four';
elsif a=5 then
t := 'five';
elsif a=6 then
t := 'six';
elsif a=7 then
t := 'seven';
elsif a=8 then
t := 'eight';
elsif a=9 then
t := 'nine';
else
t := 'zero';
end if;
dbms_output.put_line(a || ' = ' || t);
end;
Enter value for a: 2
old 5: a:=&a;
new 5: a:=2;
2 = two
PL/SQL procedure successfully completed.
P6 . Write a program to check the given number is +ve or ve :
SQL>
declare
n number(2):=12;
begin
if n>0 then
dbms_output.put_line('the given number is positive' || n);
else
dbms_output.put_line('the given number is negative' || n);
end if;
end;
/
SQL> save posneg.sql
Created file posneg.sql
SQL >The given number is positive 12
PL/SQL procedure successfully completed.
PROGRAM BASED ON NESTED IF LOOP
P7. WRITE A PROGRAM TO INPUT 2 NUMBERS IF THE 1st No >2nd No THEN SWAP IT, E
LSE IF 1st No < 2nd No RAISE IT TO ITS POWER , ELSE DOUBLES IT.
declare
a number(5);
b number(5);
t number(5);
begin
a := &a;
b := &b;
dbms_output.put_line( 'initial value of a = '||a ||' b = '||b )
if a>b
then
t:= a;
a:= b;
b:=t;
elsIF A<B
then
a:=a**a;
b:=b**b;
ELSE
a:=2*a;
b:=2*b;
end if;
dbms_output.put_line('final value of a = '||a ||' b = '||b);
end;
SQL> /
Enter value for a: 4
old 6: a:=&a;
new 6: a:=4;
Enter value for b: 5
old 7: b:=&b;
new 7: b:=5;
initial value of a = 4 b = 5
final value of a = 256 b = 3125
PL/SQL procedure successfully completed.
SQL> /
Enter value for a: 5
old 6: a:=&a;
new 6: a:=5;
Enter value for b: 4
old 7: b:=&b;
new 7: b:=4;
initial value of a = 5 b = 4
final value of a = 4 b = 5
PL/SQL procedure successfully completed.
SQL> Enter value for a: 5
old 6: a:=&a;
new 6: a:=5;
Enter value for b: 5
old 7: b:=&b;
new 7: b:=5;
initial value of a = 5 b = 5
final value of a = 10 b = 10
PL/SQL procedure successfully completed.
P8. A bank accepts fixed deposits for one or more years and the policy it ad
opts on interest is as follows:
If a deposit is < Rs 2000 and for 2 or more years , the interest rat
e is 5% compounded annually.
If a deposit is Rs.2000 or more but less than 6000 and for 2 or more
years , the interest is 7 % compounded annually.
If a deposit is Rs.6000 and for 1 or more years , the interest is 8
% compounded annually.
On all deposits for 5 years or more , interest is 10% compounded ann
ually.
On all other deposits not covered by above conditions , the interest
is 3% compounded annually.
Given the amount deposited and the number of years , Write a program to calc
ulate the amount on maturity.
declare
p number(9,2);
r number(9,2);
t number(9,2);
ci number(9,2);
begin
p := &p;
t := &t;
if p<2000 and t>=2 then
r := 5;
elsif p>=2000 and p<6000 and t>=2 then
r := 7;
elsif p>6000 and t>=1 then
r := 8;
elsif t>=5 then
r := 10;
else
r := 3;
end if;
ci := p*(1+r/100)**t - p;
dbms_output.put_line('The Principal amount is ='||p);
dbms_output.put_line('The rate of interest is ='||r);
dbms_output.put_line('The time period is ='||t);
dbms_output.put_line('The compund interst is ='||ci);
end;
P9. WRITE A PROGRM TO INPUT 3 NUMBERS FIND THE 1st Greatest, 2nd Greatest, 3
rd Greatest.
declare
a number(3);
b number(3);
c number(3);
f number(3);
s number(3);
t number(3);
begin
a :=&a;
b :=&b;
c :=&c;
if a>b and a>c then
f:= a;
if b>c then
s:=b;
t:=c;
else
s:=c;
t:=b;
end if;
elsif b>a and b>c then
f:= b;
if a>c then
s:=a;
t:=c;
else
s:=c;
t:=a;
end if;
else
f:= c;
if a>b then
s:=a;
t:=b;
else
s:=b;
t:=a;
end if;
end if;
dbms_output.put_line('first largest = ' ||f);
dbms_output.put_line('second largest = ' ||s);
dbms_output.put_line('third largest = ' ||t);
end;
/
Enter value for a: 5
old 9: a:=&a;
new 9: a:=5;
Enter value for b: 8
old 10: b:=&b;
new 10: b:=8;
Enter value for c: 9
old 11: c:=&c;
new 11: c:=9;
first largest = 9
second largest = 8
third largest = 5
PL/SQL procedure successfully completed.
P10 . WRITE A PROGRAM TO INPUT 2 NUMBERS AND AN OPERATOR , AND DISPLAY THE R
ESULT.
declare
a number(3) ;
b number(3) ;
c number(3) ;
op char(1) ;
begin
a := &a ;
b := &b ;
op := &op ;
if op='+'
then
c:=a+b;
elsif op='-'
then
c:=a-b;
elsif op='*'
then
c:=a*b;
else
c:=a/b;
end if;
dbms_output.put_line('result='||c);
end;
Enter value for a: 5
old 7: a:=&a;
new 7: a:=5;
Enter value for b: 6
old 8: b:=&b;
new 8: b:=6;
Enter value for op: '*'
old 9: op:=&op;
new 9: op:='*';
result=30
PL/SQL procedure successfully completed.
P13. Write a program to find the sum of the digits of the number:
DECLARE
N number ;
S NUMBER :=0;
R NUMBER;
begin
n:=&N;
WHILE N<>0 LOOP
R := MOD(N,10);
S := S + R;
N := TRUNC(N/10);
end loop;
dbms_output.put_line('THE SUM OF THE DIGITS = ' || S);
end;
SQL> Enter value for n: 375
old 7: n:=&N;
new 7: n:=375;
THE SUM OF THE DIGITS = 15
PL/SQL procedure successfully completed.
P30. WRITE PL/SQL SCRIPT TO CREATE A RECORD TYPE THAT CAN HOLD SALES IN 4 DI
FFERENT QUARTERS.
Structure of the table :
SALES ( SNO, NAME, QUAD1, QUAD2, QUAD3, QUAD4 )
SALES2004 ( Q1, Q2, Q3, Q4 )
DECLARE
TYPE SALEREC IS RECORD
( Q1 NUMBER,
Q2 NUMBER,
Q3 NUMBER,
Q4 NUMBER,
) ;
BEGIN
SREC SALEREC ;
SELECT SUM(QUAD1) , SUM(QUAD2) , SUM(QUAD3), SUM(QUAD4) INTO SREC FROM SALES
;
INSERT INTO SALES2004 VALUES (SREC.Q1 ,SREC.Q2, SREC.Q3, SREC.Q4);
END;
P32 . GIVEN A TABLE STUDENT ( SID INTEGER PRIMARY KEY, NAME CHAR(30), AGE IN
TEGER , GPA FLOAT ). WRITE A PL/SQL SCRIPT TO GO THROUGH SID 142-857 AND SET ALL
GPA UNDER 4.0 TO 4.0 .
DECLARE
TSID STUDENT.SID%TYPE;
TGPA STUDENT.GPA%TYPE;
BEGIN
TSID:=142;
LOOP
EXIT WHEN TSID > 857 ;
SELECT GPA INTO TGPA FROM STUDENT WHERE SID=TSID;
IF TGPA< 4.0 THEN
UPDATE STUDENT SET GPA=4.0 WHERE SID=TSID;
END IF;
TSID:=TSID+1;
END LOOP;
END;
P33 . DISPLAY AVERAGE SALARY , TOTOL NO: OF EMP, AND TOTAL SALARY FOR FIRST
5 DEPARTMENTS IN EMP TABLE. DEPT NO ARE 10,20,30,40,5090.
DECLARE
DNO NUMBER:=10;
DPNAME DEPT. DNAME%TYPE;
ASAL EMP. SAL%TYPE;
ACOUNT NUMBER;
SSAL EMP. SAL%TYPE;
BEGIN
LOOP
EXIT WHEN DNO > 50;
SELECT DNAME INTO DPNAME FROM DEPT WHERE DEPTNO=DNO;
SELECT AVG(SAL), COUNT(*), SUM(SAL) INTO ASAL, ACOUNT,SSAL
FROM EMP WHERE DEPTNO = DNO;
DBMS_OUTPUT.PUT_LINE( DEPARTMENT NAME || DPNAME);
DBMS_OUTPUT.PUT_LINE( DEPARTMENT COUNT || ACOUNT);
DBMS_OUTPUT.PUT_LINE( AVERAGE SALARY || ASAL);
DBMS_OUTPUT.PUT_LINE( SUM SALARY || SSAL);
DNO := DNO+10;
END LOOP;
END;
P34. WRITE A PL/SQL SCRIPT TO COUNT THE NO: OF EMPLOYEES. IF TOTAL <10 DISPL
AY SMALL COMPANY, ELSE IF TOTAL BETWEEN 10-50 DISPLAY MEDIUM ELSE DISPLAY LARGE
.
DECLARE
TOT NUMBER (3);
BEGIN
SELECT COUNT(*) INTO TOT FROM EMP;
IF TOT <10 THEN
TEXT := SMALL ;
ELSIF TOT<50 THEN
TEXT := MEDIUM;
ELSE
TEXT := LARGE;
END IF;
DBMS_OUTPUT.PUT_LINE( TEXT || COMPANY);
END;
P35. Find the employee drawing minimum salary. Add his details like empno, n
ame, sal into the table newsal, after incrementing Rs.700.
DECLARE
ENO EMP.EMPNO%TYPE;
NAME EMP.ENAME%TYPE;
SALARY EMP.SAL%TYPE;
BEGIN
SELECT EMPNO, ENAME, SAL INTO ENO, NAME, SALARY
FROM EMP WHERE SAL = (SELECT MIN(SAL) FROM EMP);
SALARY := SALARY+700;
INSERT INTO NEWSAL VALUES ( ENO,NAME,SALARY);
END;
P36. Write a program to update the salary of the employee by 25% if he earns
salary >4000, otherwise if salary <4000 update by 10%, else update by 15%.
DECLARE
S NUMBER(8,2) ;
NAME VARCHAR2(10);
BEGIN
NAME:=&NAME;
SELECT SAL INTO S FROM EMP WHERE ENAME = NAME;
IF S >4000 THEN
UPDATE EMP SET SAL := SAL+SAL*20/100 WHERE ENAME=NAME;
ELSIF S<4000
UPDATE EMP SET SAL := SAL+SAL*10/100 WHERE ENAME=NAME;
ELSE
UPDATE EMP SET SAL := SAL+SAL*15/100 WHERE ENAME=NAME;
END IF;
END;
P37 .Write PL/SQL code to increase the sal of an employee by 5% whose salary
is more than 4000 . Get the empno from the user.
DECLARE
ENO NUMBER(4);
BEGIN
ENO := &ENO;
UPDATE EMP SET SAL =SAL+SAL*5/100 WHERE SAL>4000 AND EMPNO=ENO;
END;
P38. GIVEN A TABLE TEMP ( SNO NUMBER(3) , DNO NUMBER,TEXT CHAR (4) . WRITE A
PL/SQL SCRIPT TO INSERT 10 RECORDS IN TABLE TEMP AS PER THE FOLLOWING SPECIFICA
TIONS:
SNO DNO TEXT
1 50 ODD
2 100 EVEN
3 150 ODD
DECLARE
SNO NUMBER := 1;
DNO NUMBER ;
TEXT CHAR(5) ;
BEGIN
LOOP
EXIT WHEN SNO > 10;
DNO := SNO * 50;
IF MOD ( SNO, 2 ) = 0
TEXT := EVEN;
ELSE
TEXT := ODD;
INSERT INTO TEMP VALUES (SNO,DNO,TEXT);
SNO := SNO + 1;
END LOOP;
COMMIT;
END;
P39 . Write PL/SQL code to DELETE the record of an employee whose salary is
more than 4000 . Get the empno from the user.
DECLARE
ENO NUMBER(4);
BEGIN
ENO := &ENO;
DELETE FROM EMP WHERE SAL>4000 AND EMPNO=ENO;
END;
P40 .Write PL/SQL code to insert a new record in the table emp after obtaini
ng values ( empno, ename, hiredate, sal ) from user.
declare
tempno number(4);
tename varchar(10);
thiredate varchar2(12);
tsal number(7,2);
begin
tempno :=&tempno;
tename :='&tename';
thiredate :='&thiredate';
tsal := &tsal;
insert into emp (empno,ename,hiredate,sal) values(tempno,tename,to_date(thir
edate,'DD-mon-yyyy'),tsal);
end;
Enter value for tempno: 8000
old 7: tempno:=&tempno;
new 7: tempno:=8000;
Enter value for tename: mathew
old 8: tename :='&tename';
new 8: tename :='mathew';
Enter value for thiredate: 10-jan-2004
old 9: thiredate :='&thiredate';
new 9: thiredate :='10-jan-2004';
Enter value for tsal: 8000
old 10: tsal := &tsal;
new 10: tsal := 8000;
PL/SQL procedure successfully completed.
P41. WRITE A PROGRAM TO CREATE A EMP %ROWTYPE RECORD .ACCEPT THE EMPNO FROM
THE USER, AND DISPLAY ALL THE INFORMATION ABOUT THE EMPLOYEE.
declare
erec emp%rowtype;
eno emp.empno%type;
begin
eno := &eno;
select * into erec from emp where empno=eno;
dbms_output.put_line( 'Emp no : '||erec.empno) ;
dbms_output.put_line( 'Name : '||erec.ename) ;
dbms_output.put_line( 'Salary : '||erec.sal);
dbms_output.put_line( 'Deptno : '||erec.deptno);
dbms_output.put_line( 'hiredate: '||erec.hiredate);
dbms_output.put_line( 'job : '||erec.job);
end;
SQL> /
Enter value for eno: 7788
old 5: eno := &eno;
new 5: eno := 7788;
Emp no : 7788
Name : SCOTT
Salary : 1452
Deptno : 20
hiredate: 19-APR-87
job : ANALYST
PL/SQL procedure successfully completed.
P45. WRITE A PROCEDURE TO INCREASE THE SALARY FOR THE SPECIFIED EMPLOLEE
USING EMPNO IN THE EMP TABLE BASED ON THE FOLLOWING CRITERIA: INCREASE THE S
ALARY BY 5% FOR CLERKS, 7% FOR SALESMAN , 10% FOR ANALYST, 20 % FOR MANAGER and
25% FOR PRESIDENT. ACTIVATE USING PL/SQL BLOCK.
SQL> SELECT * FROM EMP;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPT
7369 SMITH CLERK 7902 17-DEC-80 800 20