PLSQL Function Procedure Cursor Trigger More Programs
PLSQL Function Procedure Cursor Trigger More Programs
DECLARE
declaration statements;
BEGIN
executable statements
EXCEPTIONS
exception handling statements
END;
/
example
BEGIN
null;
END;
/
to print a text
BEGIN
dbms_output.put_line(var);
END;
/
DECLARE
num NUMBER;
BEGIN
num := &Enter_Number;
IF MOD(num, 2) = 0 THEN
DBMS_OUTPUT.PUT_LINE(num || ' is
Even.');
ELSE
DBMS_OUTPUT.PUT_LINE(num || ' is
Odd.');
END IF;
END;
/
Armstrong number
declare
n number:=1634;
s number:=0;
r number;
len number;
m number;
begin
m := n;
len := length(to_char(n));
while n>0
loop
r := mod(n , 10);
s := s + power(r , len);
n := trunc(n / 10);
end loop;
if m = s
then
dbms_output.put_line('yes');
else
dbms_output.put_line('no');
end if;
end;
fibonocci program
declare
a number := 0;
b number := 1;
temp number;
n number;
i number;
begin
n:=&n;
dbms_output.put_line(a);
dbms_output.put_line(b);
for i in 2..n
loop
temp:= a + b;
a := b;
b := temp;
dbms_output.put_line(temp);
end loop;
end;
DECLARE
num number;
factorial number;
RETURN number
IS
f number;
BEGIN
IF x=0 THEN
f := 1;
ELSE
f := x * fact(x-1);
END IF;
RETURN f;
END;
BEGIN
num:= 6;
factorial := fact(num);
END;
Questions
1. Check if a Year is a Leap Year
2. Find the Largest of Three Numbers
3. Determine Voting Eligibility
4. Check if a Character is a Vowel or Consonant
5. Print First N Natural Numbers
6. Print Multiplication Table of a Given Number
7. Print Even Numbers from 1 to N
8. Reverse Order Printing of numbers
1. PL/SQL block for inserting rows into EMPDET table with the following calculations:
HRA=50% OF BASIC
DA=20% OF BASIC
PF=7% OF BASIC
NETPAY=BASIC+DA+HRA-PF
INPUT:
DECLARE
ENO1 empdet.eno%type;
ENAME1 empdet.name%type;
DEPTNO1 empdet.deptno%type;
BASIC1 empdet.basic%type;
HRA1 empdet.HRA%type;
DA1 empdet.DA%type;
PF1 empdet.pf%type;
NETPAY1 empdet.netpay%type;
BEGIN
ENO1:=&ENO1;
ENAME1:='&ENAME1';
DEPTNO1:=&DEPTNO1;
BASIC1:=&BASIC1;
HRA1:=(BASIC1*50)/100;
DA1:=(BASIC1*20)/100;
PF1:=(BASIC1*7)/100;
NETPAY1:=BASIC1+HRA1+DA1-PF1;
INSERT INTO EMPDET
VALUES(ENO1,ENAME1,DEPTNO1,BASIC1,HRA1,DA1,PF1,NETPAY1);
end;
OUTPUT:
SQL> @BASIC
Enter value for eno1: 104
old 11: ENO1:=&ENO1;
new 11: ENO1:=104;
Enter value for ename1: SRINIVAS REDDY
old 12: ENAME1:='&ENAME1';
new 12: ENAME1:='SRINIVAS REDDY';
Enter value for deptno1: 10
old 13: DEPTNO1:=&DEPTNO1;
new 13: DEPTNO1:=10;
Enter value for basic1: 6000
old 14: BASIC1:=&BASIC1;
new 14: BASIC1:=6000;
SQL>/
Enter value for eno1: 105
old 11: ENO1:=&ENO1;
new 11: ENO1:=105;
Enter value for ename1: CIRAJ
old 12: ENAME1:='&ENAME1';
new 12: ENAME1:='CIRAJ';
Enter value for deptno1: 10
old 13: DEPTNO1:=&DEPTNO1;
new 13: DEPTNO1:=10;
Enter value for basic1: 6000
old 14: BASIC1:=&BASIC1;
new 14: BASIC1:=6000;
INPUT:
DECLARE
num number(5);
rem number;
BEGIN
num:=#
rem:=mod(num,2);
if rem=0
then
dbms_output.put_line(' Number '||num||' is Even');
else
dbms_output.put_line(' Number '||num||' is Odd');
end if;
end;
OUTPUT:
SQL>start even
Enter value for num: 6
old 5: num:=#
new 5: num:=6;
Number 6 is Even
SQL> /
Enter value for num: 3
old 5: num:=#
new 5: num:=3;
Number 3 is Odd
INPUT:
DECLARE
num number(5);
rem number(5);
sm number(5):=0;
num1 number(5);
BEGIN
num:=#
num1:=num;
while(num>0)
loop
rem:=mod(num,10);
sm:=sm+rem;
num:=trunc(num/10);
end loop;
dbms_output.put_line('SUM OF DIGITS OF '||num1||' IS: '||Sm);
end;
/
OUTPUT:
SQL> @sum
INPUT truncated to 2 characters
Enter value for num: 123
old 7: num:=#
new 7: num:=123;
SUM OF DIGITS OF 123 IS: 6
SQL> @sum
INPUT truncated to 2 characters
Enter value for num: 456
old 7: num:=#
new 7: num:=456;
SUM OF DIGITS OF 456 IS: 15
INPUT:
DECLARE
num number(5);
fact number(5):=1;
k number(5);
BEGIN
num:=#
k:=num;
while(num>0)
loop
fact:=fact*num;
num:=num-1;
end loop;
dbms_output.put_line('FACTORIAL OF '||k||' IS '||fact);
end;
OUTPUT:
SQL>@fact
Enter value for num: 5
old 6: num:=#
new 6: num:=5;
FACTORIAL OF 5 IS 120
SQL> /
Enter value for num: 6
old 6: num:=#
new 6: num:=6;
FACTORIAL OF 6 IS 720
INPUT:
DECLARE
num number(5);
f1 number(5):=0;
f2 number(5):=1;
f3 number(5);
i number(5):=3;
BEGIN
num:=#
dbms_output.put_line('THE FIBONACCI SERIES IS:');
dbms_output.put_line(f1);
dbms_output.put_line(f2);
while(i<=num)
loop
f3:=f1+f2;
dbms_output.put_line(f3);
f1:=f2;
f2:=f3;
i:=i+1;
end loop;
end;
/
OUTPUT:
Programs on Functions
INPUT:
declare
num number;
count1 number;
begin
num:=#
count1:=prime(num);
if count1>2 then
dbms_output.put_line(num||''||'is not a prime number');
else
dbms_output.put_line(num|| ''||'is prime');
end if;
end;
INPUT:
declare
n p.p#%type;
i number;
begin
n:='&n';
i:=ex(n);
if i=1 then
dbms_output.put_line('given'||''||n||'is in the table');
end if;
exception
when no_data_found then
dbms_output.put_line('given'||''||n||'is not in the table');
end;
Programs on Packages
c) To insert an Employee
c) To delete an Employee
c) To List employees in a given Dept (Deptno/Dept name)
Package Specification:
Package Body:
Procedure
1.Write a procedure to update salary of given employee by 10%
2. Write a procedure to count number of students taken the given course
3.Write a procedure to list the number courses taken by the given instructor
4. Write a procedure to find the sum of salaries of employees belongs to given department
name
Functions
1. Write a function to find the given number is palindrome or not
2. Write a function to count number of students taken the given course
3. Write a function to average salary of the given department
4. Write a function to find the employees working under given manager
Create a student package which find the total courses, total credits ,name of the course opted by
the student
Cursors
declare
cursor c1(j varchar2,dn number) is select empno,ename from emp where
job=j and deptno=dn;
row1 emp%rowtype;
jbemp.job%type;
d emp.deptno%type;
begin
jb:='&jb';
d:=&d;
open c1(jb,d);
fetch c1 into row1.empno,row1.ename;
if c1%notfound then
dbms_output.put_line('Employee does not exist');
else
dbms_output.put_line('empno is:'||row1.empno||'' ||'employee name is:'||
row1.ename);
end if;
end;
declare
cursor c is select empno,ename,deptno,sal from emp ;
iemp.empno%type;
j emp.ename%type;
k emp.deptno%type;
l emp.sal%type;
begin
open c;
dbms_output.put_line('Empno,name,deptno,salary of employees are:= ');
loop
fetch c into i,j,k,l;
exit when c%notfound;
dbms_output.put_line(i||''||j||''||k||''||l);
end loop;
close c;
end;
Write a PL/SQL program to list employee names whoseslary is more than their Manager
( to whom the/report) salary.
declare
cursor c1 is
select e1.ename,e1.sal,e2.ename,e2.sal from emp e1,
emp e2 where e1.mgr=e2.empno and e1.sal>e2.sal;
e1 emp.ename%type;
e2 emp.sal%type;
e3 emp.ename%type;
e4 emp.sal%type;
begin
open c1;
loop
fetch c1 into e1,e2,e3,e4;
exit when c1%notfound;
dbms_output.put_line(e1||'---'||e2||'---'||e3||'---'||e4);
end loop;
close c1;
end;
Implement a PL/SQL program to list names of Employees with the position the position of
employee in the list sorted by salary in decreasing order.
declare
sal1 emp.sal%type;
c number;
cursor c1 is
select ename from emp order by ename;
cursor c2 is
select ename,sal from emp order by sal desc;
begin
for i in c1 loop
for j in c2 loop
c:=c2%rowcount;
if(i.ename=j.ename) then
dbms_output.put_line(i.ename||' '||c);
end if;
end loop;
end loop;
end;
Declare
cursor c1 is select empno,ename,mgr from emp;
enoemp.empno%type;
mnoemp.mgr%type;
enaemp.ename%type;
begin
dbms_output.put_line(‘king’);
for m in c1 loop
eno:=m.empno;
mno:=m.mgr;
while mno is not null
loop select ename into ena from emp where empno=eno;
dbms_output.put_line(ena||’—‘);
select mgr into mno from emp where empno=eno;
eno:=mno;
end loop;
dbms_output.put_line(‘—‘);
end loop;
end;
1. Write cursor to increase the employee salary by 50% if his current salary is more than
15000 else increase by 20%
2. Write a program in PL/SQL to print a list of managers and the name of the departments.
3. Write a program in PL/SQL to insert data into two tables from one table using cursor.
4. Write a program in PL/SQL to create a cursor displays the name and salary of each
employee in the EMPLOYEES table whose salary is less than that specified by a passed-
in parameter value.
Triggers
1. Create the following triggers.
Write a TRIGGER Programme to Ensure That DEPT TABLE Does not contain
DUPLICATE OR NULL VALUES IN DEPTNO.
INPUT
create or replace trigger trig1 before insert on dept for each row
declare
a number;
begin
if(:new.deptno is Null) then
raise_application_error(-20001,'error::deptno cannot be null');
else
select count(*) into a from dept where deptno=:new.deptno;
if(a=1) then
raise_application_error(-20002,'error:: cannot have duplicate deptno');
end if;
end if;
end;