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

PLSQL

The document contains examples of PL/SQL code snippets for different programming practices. It includes blocks to check even/odd numbers, display grades, handle exceptions, use implicit cursors and more. Multiple code examples are provided for each practice.

Uploaded by

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

PLSQL

The document contains examples of PL/SQL code snippets for different programming practices. It includes blocks to check even/odd numbers, display grades, handle exceptions, use implicit cursors and more. Multiple code examples are provided for each practice.

Uploaded by

rohanggupta9087
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Slip1:-

1]Prac-2A :Write a PL/SQL block to check if the value of the given


number is even or odd number.
code: Declare
x int;
Begin
x := &x;
if mod(x,2) = 0 then
dbms_output.put_line('The Number '||x||'is even.');
else
dbms_output.put_line('The Number '||x||'is odd.');
end if;
end;
/

2] Prac-5:-Program using GOTO label:- write a PL/SQL block to display


numbers from 1 to 20 and skip if the number is 11 using while loop.
code :

declare
a number :=0;
begin
<<loopstart>>
a := a + 1;
while (a<=20)
loop
if (a = 11) then
goto loopstart;
end if;
dbms_output.put_line(a);
a := a + 1;
end loop;
end;
Slip2:-
1] Prac-2B]:-Write a PL/SQL block to check if the given number is
zero or positive or negative.
code:
declare
x int ;
Begin
x := &x;
if (x>0) then
dbms_output.put_line('The number'||x||' is Positive Number');
elsif(x <0) then
dbms_output.put_line('The number'||x||' is Negative Number');
else
dbms_output.put_line('The number is zero');

end if;
end;
/

2] Prac-11(a):-Write a row-level trigger to display the difference in


balance from account table (acc_no, acc_holder_name, balance) when
we try to fire insert or update or delete operations (DML operations) in
the said table.
Note:
1. Create account table (acc_no, acc_holder_name, balance) if not already exits.
2. Insert 5 records for the above table.
3. Run the PL/SQL code (trigger should be created)
4. Check trigger for insert query.
5. Check trigger for update query.

CODE:
create table account(acc_no int ,acc_holder_name varchar(50) ,balance int);
insert into account values (100,'abc',1000);
insert into account values (101,'efg',2000);
insert into account values (102,'hij',4000);
insert into account values (103,'klm',8000);
insert into account values (104,'nop',10000);
select* from account;
create trigger display_bal_changes3
before delete or insert or update on account
for each row
when (new.acc_no> 0)
declare
bal_diff number;
begin
bal_diff := :new.balance - :old.balance;
dbms_output.put_line('old balance: ' || :old.balance);
dbms_output.put_line('new balance: ' || :new.balance);
dbms_output.put_line('balance Amount Difference: ' || bal_diff);
end;

insert into account values(105,'pqr',5000);

update account set balance = 10000 where acc_no = 100;

Slip3:-
1] prac 2C :Write a PL/SQL block to check the percentage of the student
and display the grade as follows:
>=75 grade O

>=65 grade A

>=50 grade B

>=40 grade C

<40 fail

code:
DECLARE
total_marks int ;
obtained_marks int ;
percentage int;
BEGIN
total_marks := &total_marks;
obtained_marks := &obtained_marks ;
percentage := (obtained_marks / total_marks) * 100;

IF percentage >= 75 THEN


DBMS_OUTPUT.PUT_LINE('Grade: O');
ELSIF percentage >= 65 THEN
DBMS_OUTPUT.PUT_LINE('Grade: A');
ELSIF percentage >= 50 THEN
DBMS_OUTPUT.PUT_LINE('Grade: B');
ELSIF percentage >= 40 THEN
DBMS_OUTPUT.PUT_LINE('Grade: C');
ELSE
DBMS_OUTPUT.PUT_LINE('Fail');
END IF;
END;
/

2] Prac-11(b):-Write a row-level trigger to display the difference in


balance from account table (acc_no, acc_holder_name, balance) when
we try to fire insert or update or delete operations (DML operations) in
the said table using procedure.

Note:
1. Create account table (acc_no, acc_holder_name, balance) if not already exits.
2. Insert 5 records for the above table.
3. Run the PL/SQL code (trigger should be created) (This step is done in practical
11 - A)
4. Write a PL/SQL code to get the old balance, new balance and balance
difference after the trigger is created.

code :
create table account(acc_no int ,acc_holder_name varchar(50) ,balance int);
insert into account values (100,'abc',1000);
insert into account values (101,'efg',2000);
insert into account values (102,'hij',4000);
insert into account values (103,'klm',8000);
insert into account values (104,'nop',10000);
select* from account;
declare
total_rows number;
begin
update account set balance = balance + 5000;
if sql%notfound then
dbms_output.put_line('No acocunt updated');
elsif sql%found then
total_rows := sql%rowcount;
dbms_output.put_line(total_rows || 'account updated');
end if;
end;

Slip4:-
1] prac 2D:Write a PL/SQL block to check if a given character is a vowel
or consonant.

code:
DECLARE
x varchar(1);

BEGIN
x := '&x';
IF x in ('a' ,'e' ,'i','o','u','A' ,'E' ,'I' ,'O' ,'U') THEN
DBMS_OUTPUT.PUT_LINE('The character ' || x || ' is a vowel.');
ELSE
DBMS_OUTPUT.PUT_LINE('The character ' || x || ' is a consonant.');
END IF;
END;

2] Prac-10(a):-Write a PL/SQL block to demonstrate build-in exception


using customer table.

Note:
1. Create customer table (id, name, age, address, phone_no).
2. Insert 6 records in customer table.
3. Using PL/SQL block select a particular customer id (that is present in the
schema) and display the same.
4. Using PL/SQL block select a particular customer id (that is not present in the
schema) and
display the same. Check if the exception block is activated in this case.
Code:
create table customer(id int ,name varchar(50),age varchar(50) ,address
varchar(100),phone_no varchar(50));
insert into customer values(1 , 'sushil' , 19,'thane', 9876543210);
insert into customer values(2 , 'shawn' , 18,'mira road', 9876678210);
insert into customer values(3 , 'Ashutosh' , 20 ,'Vasai', 9702420582);
insert into customer values(4 , 'Anish' , 19 ,'Mira Road', 9702344582);
insert into customer values(5 , 'Saurav' , 18 ,'Mira Road', 9702344454);
insert into customer values(6 , 'Anushka' , 15 ,'Dadar', 9756424546);
select * from customer;
declare
c_id customer.id%type := &c_id;
c_name customer.name%type;
c_age customer.age%type;
c_add customer.address%type;
c_pno customer.phone_no%type;
begin
select name, address into c_name, c_add from customer where id =
c_id;
dbms_output.put_line('Customer Name: ' || c_name);
dbms_output.put_line('Customer Address: ' || c_add);
exception
when no_data_found then
dbms_output.put_line('No such customer');
when others then
dbms_output.put_line('ERROR');
end;

Slip 5:-
2] prac 2E:Write a PL/SQL block to display details of the grade obtained
by the student using case.
O - Excellent
A - Very good
B - Good
C - Average
D - Need to Improve
F - Fail
code:
DECLARE
grade char(1) ;
BEGIN
grade := '&grade';
case grade
when 'O' then dbms_output.put_line('Your grade is Excellent ');
when 'A' then dbms_output.put_line('Your grade is Very Good ');
when 'B' then dbms_output.put_line('Your grade is Good ');
when 'C' then dbms_output.put_line('Your grade is Average ');
when 'D' then dbms_output.put_line('Your grade is Need to improve
');
else
dbms_output.put_line('You are fail');
end case;
END;
/

2] Prac-10(b):-Write a PL/SQL block to demonstrate build-in exception


(too_many_rows) using library table.
1. Create library table (b_no, b_name, b_author).
2. Insert 5 records in library table, where book no should be repeated for
atleast more than one book.
3. Using PL/SQL block select a particular book no(that is present only
once in the schema) and display the same.
4. Using PL/SQL block select a particular book no(that is present more
than once in the schema) and
display the same. Check if the exception block is activated in this case.

Code:
create table library(b_no int ,b_name varchar(50),b_author varchar(50));
insert into library values(1 ,'Gitanjali','Rabindranath Tagore');
insert into library values(2 ,'Gita Rahasya','Bal Gangadhar tilak');
insert into library values(3 ,'Ignited mind','APJ Abdul Kalam');
insert into library values(4 ,'Hinduism','Nirad C. Chaudhuri');
select * from library;
declare
id library.b_no%type :=&book_id;
name library.b_name%type;
author library.b_author%type;
begin
select b_name, b_author into name, author from library where b_no =
id;
dbms_output.put_line('Book Name: ' || name);
dbms_output.put_line('Book Author: ' || author);
exception
when too_many_rows then
dbms_output.put_line('more than one row with same book id');
when others then
dbms_output.put_line('ERROR');
end;

Slip6:-
1] prac 3A:Write a PL/SQL block to display n natural numbers.
code:
Declare
num int;
i int:= 1;
Begin
num:=&num;
while(i<=num) loop
dbms_output.put_line(i);
i:=i+1;
end loop;
end;
/

2] Prac-8(a):- Write a PL/SQL block to demonstrate the use of different


attributes of implicit cursor in employee table by increasing the salary of
all the employees by 5000.
step 1 - create employee table
step 2 - insert 5 records in the employee table
step 3 - display all the records and check the value of salary (write this
output in the journal)
step 4 - run the PL/SQL code (output of this code in the journal)
step 5 - display all the records and check the value of salary (write this
output in the journal)
code:
create table employee(id int ,name varchar(50),salary int);
insert into employee values(1 , 'sushil' , 30000);
insert into employee values(2 , 'shawn' , 40000);
insert into employee values(3 , 'Ashutosh' , 50000;
insert into employee values(4 , 'Anish' , 60000);
insert into employee values(5 , 'Saurav' , 700000);
select * from employee;
declare
total_rows number;
begin
update employee set salary = salary + 5000;
if sql%notfound then
dbms_output.put_line('No employee selected');
elsif sql%found then
total_rows := sql%rowcount;
dbms_output.put_line(total_rows ||' employee salary
updated');
end if;
end;
/
select * from employee;

Slip 7:-
1]prac 3B:Write a PL/SQL block to display sum of n natural numbers.
Note 1+2+3+4+.....+n
code:
Declare
num int;
i int:= 1;
s int:= 0;
Begin
num := &num;
while(i<=num) loop
s := s + i;
i := i+1;
end loop;
dbms_output.put_line(s);
end;
/

2] Prac-8(b):- Write a PL/SQL block to display all the instances of


employee table using explicit cursor.

step 1 - create employee table


step 2 - insert 5 records in the employee table
step 3 - run the PL/SQL code and check if all the records from employee
table is displayed.
Steps to create explicit cursor
1. declare the cursor
2. open the cursor
3. fetch the cursor
4. close the cursor

code:
create table employee(id int ,name varchar(50),salary int);
insert into employee values(1 , 'sushil' , 30000);
insert into employee values(2 , 'shawn' , 40000);
insert into employee values(3 , 'Ashutosh' , 50000;
insert into employee values(4 , 'Anish' , 60000);
insert into employee values(5 , 'Saurav' , 700000);
select * from employee;
declare
e_id employee.emp_no%type;
e_name employee.emp_name%type;
e_sal employee.salary%type;
-- step 1 declare the cursor
cursor e_employee is
select emp_no, emp_name, salary from employee;
begin
-- step 2 open the cursor
open e_employee;
dbms_output.put_line('EMP_ID' || ' ' || 'EMP_NAME' || ' ' ||
'SALARY');
loop
-- step 3 fetch the cursor
fetch e_employee into e_id, e_name, e_sal;
exit when e_employee%notfound;
dbms_output.put_line(e_id || ' ' || e_name || ' ' || e_sal);
end loop;
-- step 4 close the cursor
close e_employee;
end;
/

Slip 8:-
1]prac 3c:Write a PL/SQL block to calculate and display factorial of n
(n!)
code:
declare
fac number :=1;
n number ;
begin
n:= &n;
while (n > 0) loop
fac:=n*fac;
n:=n-1;
end loop;
dbms_output.put_line(fac);
end;
/

2] Prac-7(a):-Write a PL/SQL block for function to calculate sum of n


natural numbers and call the same.
code:
declare
function sum_of_no return number is
no number := &no;
i number;
result number := 0;
begin
for i in 1 .. no loop
result := result + i;
end loop;
return(result);
end;
begin
dbms_output.put_line('sum of number is ' || sum_of_no());
end;
/

Slip 9:-
1] Prac 3D-Write a PL/SQL block to display the sum of square of n natural numbers.
code:

declare
n int;
sum_of_square int := 0;
Begin
n := &n;
for i in 1..n
loop
sum_of_square := sum_of_square + (i * i);
end loop;
dbms_output.put_line('Sum of Square of '||n||'Natural number is
:'||sum_of_square);
end;
/

2] Prac-7(b):-Write a PL/SQL block for recursive function to calculate


factorial of a given number.
Note: n! = n * (n-1) * (n -2) ........ * 1

code:
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 := &num;
factorial := fact(num);
dbms_output.put_line('Factorial of ' || num || ' is = ' || factorial);
end;
/

Slip 10:-
1]Prac 3E:- Write a PL/SQL block to display fibonacci series
Note: 0 1 1 2 3 5 8 12 21 .........

code:
declare
a int := 0;
b int := 1;
temp int;
n int ;
i int;
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;
2] Prac-6(a):- write a PL/SQL stored procedure to display a message.
Syntax to create a procedure
create or replace procedure procedure_name (parameter) -- parameter
is optional
code:
create or replace procedure message
is
begin
dbms_output.put_line('This is my first stored procedure code');
end;
/

execute message;

Slip 11:-
1]prac 3F:-Write a PL/SQL block to check if a given number is a prime
number or not.
code:
declare
n int;
i int;
temp int;
begin
n := &n;
i := 2;
temp := 1;
for i in 2..n/2 loop
if mod(n, i) = 0
then
temp := 0;
exit;
end if;
end loop;
if temp = 1 then
dbms_output.put_line(n||'is a Prime Number');
else
dbms_output.put_line(n||'is Not a Prime Number');
end if;
end;
/

2] Prac-6(b):- create a PL/SQL stored procedure with parameter and


write a PL/SQL block to find the greatest number among two given
numbers.
code:
declare
a number;
b number;
c number;
procedure max_no(x in number, y in number, z out number) is
begin
if (x > y) then
z := x;
else
z := y;
end if;
end;
begin
a := &a;
b := &b;
dbms_output.put_line('value of a is ' || a);
dbms_output.put_line('value of b is ' || b);
max_no(a,b,c);
dbms_output.put_line('the largest number is ' || c);
end;
/

Slip 12:-
1] Prac-4(a):-Create a sequence:-
code:
create sequence student_sequence
start with 1
increment by 1
minvalue 1
maxvalue 100
nocycle;

create table student(roll_no int primary key, name varchar(20) not null);
insert into student values (student_sequence.nextval, 'Saurav');
insert into student values (student_sequence.nextval, 'Anish');
insert into student values (student_sequence.nextval, 'Ashutosh');
insert into student values (student_sequence.nextval, 'Shawn');
insert into student values (student_sequence.nextval, 'Sushil');
select * from student;

2] Prac-6(c):-create a PL/SQL stored procedure with parameter (single


variable with IN OUT mode) and write a PL/SQL block to find the cube
of the given number using the procedure created.
code:
declare
n number := 10;
procedure cube_of_no(a in out number) is
begin
a := a * a * a;
end;
begin
dbms_output.put_line('given number is ' || n);
cube_of_no(n);
dbms_output.put_line('cube of given number is' ||n);
end;
/

Slip 13:-
prac 3G: Write a PL/SQL block to display all the prime numbers between 1 - 100.
code:

DECLARE
BEGIN
FOR i IN 2..100 LOOP
DECLARE
Prime_number int:= 0;
BEGIN
FOR j IN 2..i-1 LOOP
IF MOD (i,j) = 0 THEN
prime_number := prime_number + 1;
END IF;
END LOOP;

IF prime_number = 0 THEN
DBMS_OUTPUT.PUT_line(i );
END IF;
END;
END LOOP;

END;
/

Prac-4(b):-Create a sequence for product table (p_no pk, p_name not


null) to insert product no from 1000 to 5000. Change the sequence
value once it reaches 1500. Increment it by 500 and insert few records.
Display the same and verify if the sequence number is changed.
code:

create sequence product_sequence1


start with 1000
increment by 100
maxvalue 5000
nocycle;

create table product1 (p_no int primary key, p_name varchar(20) not null);
insert into product1 values (product_sequence1.nextval, 'pencil');
insert into product1 values (product_sequence1.nextval, 'marker');
insert into product1 values (product_sequence1.nextval, 'journal');
insert into product1 values (product_sequence1.nextval, 'book');
insert into product1 values (product_sequence1.nextval, 'pen');

select * from product;

alter sequence product_sequence1 increment by 1500;

insert into product values (product_sequence1.nextval, 'duster');

select * from product;

You might also like