0% found this document useful (0 votes)
0 views

PLSQL Function Procedure Cursor Trigger More Programs

The document provides a comprehensive overview of PL/SQL syntax and examples, including variable declarations, control structures, and various functions and procedures for tasks such as calculating factorials, generating Fibonacci series, and checking for prime numbers. It also includes examples of user input handling, exception management, and database operations like inserting and updating records. Additionally, it outlines several programming challenges and solutions related to employee management and mathematical computations.

Uploaded by

b.ganesh33399
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

PLSQL Function Procedure Cursor Trigger More Programs

The document provides a comprehensive overview of PL/SQL syntax and examples, including variable declarations, control structures, and various functions and procedures for tasks such as calculating factorials, generating Fibonacci series, and checking for prime numbers. It also includes examples of user input handling, exception management, and database operations like inserting and updating records. Additionally, it outlines several programming challenges and solutions related to employee management and mathematical computations.

Uploaded by

b.ganesh33399
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 25

syntax for PL/SQL

DECLARE
declaration statements;

BEGIN
executable statements

EXCEPTIONS
exception handling statements

END;
/
example

SQL> SET SERVEROUTPUT ON;


SQL> DECLARE
var1 INTEGER;
var2 REAL;
var3 varchar2(20) ;

BEGIN
null;
END;
/
to print a text

SQL> SET SERVEROUTPUT ON;


SQL> DECLARE
var varchar2(40) := 'I love DBMS' ;

BEGIN
dbms_output.put_line(var);

END;
/

Read the string from user

SQL>SET SERVEROUTPUT ON;


SQL>DECLARE
user_input VARCHAR2(100); -- Variable to store user input
BEGIN
-- Accept input from the user
user_input := '&Enter_String';

-- Display the user input


DBMS_OUTPUT.PUT_LINE('You entered: ' || user_input);
END;
/

check number is even or odd

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

-- declare variable n, s,r, len

-- and m of datatype number

n number:=1634;

s number:=0;

r number;
len number;

m number;

begin

m := n;

len := length(to_char(n));

-- while loop till n>0

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('fibonacci series is :');

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;

factorial by recursive function

DECLARE
num number;

factorial number;

FUNCTION fact(x 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);

dbms_output.put_line(' Factorial '|| num || ' is ' || factorial);

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;

PL/SQL procedure successfully completed.

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;

PL/SQL procedure successfully completed.

SQL> SELECT * FROM EMPDET;


OUTPUT:
ENO NAME DEPTNO BASIC HRA DA PF NETPAY
--------- ------------------------------ --------- --------- --------- --------- --------- -----------------------
101 SANTOSH 10 5000 2500 1000 350 8150
102 SHANKAR 20 5000 2500 1000 350 8150
103 SURESH 20 5500 2750 1100 385 8965
104 SRINIVASA REDDY 10 6000 3000 1200 420 9780
105 CIRAJ 10 6000 3000 1200 420 9780

2. Write a PL/SQL block to check the given number is Even or Odd

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

PL/SQL procedure successfully completed.

SQL> /
Enter value for num: 3
old 5: num:=#
new 5: num:=3;
Number 3 is Odd

PL/SQL procedure successfully completed.

3. Write a PL/SQL block to Find Sum of Digits of a given Number

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

PL/SQL procedure successfully completed.

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

PL/SQL procedure successfully completed.

4: Write a PL/SQL block to find the Factorial of a Given Number

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

PL/SQL procedure successfully completed.

SQL> /
Enter value for num: 6
old 6: num:=#
new 6: num:=6;
FACTORIAL OF 6 IS 720

PL/SQL procedure successfully completed.

5. Write a PL/SQL block to Generate Fibonacii Series

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:

SQL> start fib


Enter value for num: 10
old 8: num:=&num;
new 8: num:=10;
THE FIBONACCI SERIES IS:
0
1
1
2
3
5
8
13
21
34

PL/SQL procedure successfully completed.


SQL> /
Enter value for name1: SANTOSH
old 6: name1:='&name1';
new 6: name1:='SANTOSH';
REVERSE OF STRING IS:HSOTNAS
SANTOSH IS NOT PALINDROME

PL/SQL procedure successfully completed.


1. Get the number of employees working under a given employee.

create or replace procedure nos(e1 emp.empno%type) is


cnt number;
begin
select count(*) into cnt from emp where mgr=e1;
dbms_output.put_line(cnt);
exception
when no_data_found then
dbms_output.put_line('wrong empno');
end nos;

2. Get the number of employees working in given department name.

create or replace procedure nosd(d1 dept.dname%type) is


cnt number;
begin
select count(*) into cnt from emp,dept where
emp.deptno=dept.deptno and dname=d1;
dbms_output.put_line(cnt);
exception
when no_data_found then
dbms_output.put_line('wrong dept name');
end nosd;
3. Create a Procedure to accept an Empno, and a salary increase amount, if Empno is
not found or current salary is NULL then raise exceptions otherwise display total
salary.

create or replace procedure empis(e1 emp.empno%type,in1 emp.sal%type) is


s1 emp.sal%type;
nusal exception;
nsal emp.sal%type;
begin
select sal into s1 from emp where empno=e1;
if s1 is null then
raise nusal;
else
nsal:=s1+in1;
dbms_output.put_line(nsal);
end if;
exception
when nusal then
dbms_output.put_line('given emp. sal is null');
when no_data_found then
dbms_output.put_line('wrong empno');
end empis;

Programs on Functions

1. Write a program to check whether the given number is Prime or not.

INPUT:

create or replace function prime(n number) return number as


cnt number;
begin
cnt:=0;
for i in 1..n loop
if mod(n,i)=0 then
cnt:=cnt+1;
end if;
end loop;
return cnt;
end prime;

declare
num number;
count1 number;
begin
num:=&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;

Procedure and Function

Write a program to check for the existence of P# in the table parts

INPUT:

create or replace function ex(pno p.p#%type) return number as


pnum p.p#%type;
cnt number;
begin
cnt:=0;
select p# into pnum from p where p#=pno;
if pno=pnum then
cnt:=1;
end if;
return cnt;
end ex;

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

1. Write a package “EMPPACK” with the following function/Procedures

c) To insert an Employee
c) To delete an Employee
c) To List employees in a given Dept (Deptno/Dept name)

Package Specification:

create or replace package emppack is


procedure ins2(e1 emp1.empno%type,en emp1.ename%type,j1 emp1.job%type,
m1 emp1.mgr%type,h1 emp1.hiredate%type,s1 emp1.sal%type,
c1 emp1.comm%type,d1 emp1.deptno%type);
procedure del1(e1 emp1.empno%type);
procedure noe1(d emp.deptno%type);
end emppack;

Package Body:

create or replace package body emppack is

procedure ins2(e1 emp1.empno%type,en emp1.ename%type,j1 emp1.job%type,


m1 emp1.mgr%type,h1 emp1.hiredate%type,s1 emp1.sal%type,
c1 emp1.comm%type,d1 emp1.deptno%type) is
begin
insert into emp1 values(e1,en,j1,m1,h1,s1,c1,d1);
end ins2;
procedure del1(e1 emp1.empno%type) is
begin
delete from emp1 where empno=e1;
end del1;

procedure noe1(d emp.deptno%type) is


c number(2);
begin
select count(empno) into c from emp where deptno=d;
dbms_output.put_line(c);
end noe1;
end emppack;

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

Programs on implicit cursors attributes


begin
update emp1 set sal=&sal from emp where deptno=&deptno;
if SQL%FOUND then
dbms_output.put_line(' emp Successfully updated ');
end if;
if SQL%NOTFOUND then
dbms_output.put_line(' emp not Successfully updated ');
end if;
end;
declare
rows char(4);
begin
update emp1 set sal=&sal where deptno=&deptno;
rows:=to_char(SQL%ROWCOUNT);
dbms_output.put_line(rows);
end;

Program to display the list of Employees and Total Salary


Department wise.
INPUT:
declare
cursor c1 is select * from dept;
cursor c2 is select * from emp;
s emp.sal%type;
begin
for i in c1
loop
s:=0;
dbms_output.put_line('----------------------------------------------');
dbms_output.put_line('Department is :' || i.deptno ||' Department name is:' ||
i.dname);
dbms_output.put_line('-------------------------------------------');
for j in c2
loop
if ( i.deptno=j.deptno) then
s:=s+j.sal;
dbms_output.put_line(j.empno|| ''|| j.ename || ''|| j.sal );
end if;
end loop;
dbms_output.put_line('----------------------------------------------');
dbms_output.put_line('Total salary is: '|| s);
dbms_output.put_line('----------------------------------------------');
end loop;
end;
PL/SQL Block Using CURSOR TO Find EMPLOYEE WITH GIVEN JOB
AND DEPTNO

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;

Write a Cursor to display the list of employees who are working as a


Managers or Analyst
INPUT:
declare
cursor c(jb varchar2) is select ename from emp where job=jb;
ememp.job%type;
begin
open c('MANAGER');
dbms_output.put_line(' EMPLOYEES WORKING AS MANAGERS ARE:');
loop
fetch c into em;
exit when c%notfound;
dbms_output.put_line(em);
end loop;
close c;
open c('ANALYST');
dbms_output.put_line(' EMPLOYEES WORKING AS ANALYST ARE:');
loop
fetch c into em;
exit when c%notfound;
dbms_output.put_line(em);
end loop;
close c;
end;

write a Cursor to display List of Employees from Emp Table in


PL/SQL block

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;

List ename, manager chain for each employee as follows

SMITH -------FORD ------ JONES ------ KING

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.

a) Insertion of an employee must be possible only on Saturday between 10 AM


and 5 PM.

create or replace trigger emp_ins before


insert on emp1 for each row
begin
if to_char(sysdate,'dy')!='mon' and
to_char(sysdate,'hh') not between 10 and 17 then
raise_application_error(-20007,'it is not possible');
end if;
end;

b) Salary of an employee must be modified only on Monday in any


month with an entry in the log table.

create or replace trigger emp_up before update


of sal on emp1 for each row
begin
if to_char(sysdate,'dy')!='mon' then
raise_application_error(-20007,'it is not possible');
else
insert into log values(:old.empno,:old.ename,:old.sal,:new.sal,sysdate);
end if;
end;

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;

You might also like