PLSQL
PLSQL
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;
/
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;
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;
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;
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;
/
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:=#
while(i<=num) loop
dbms_output.put_line(i);
i:=i+1;
end loop;
end;
/
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 := #
while(i<=num) loop
s := s + i;
i := i+1;
end loop;
dbms_output.put_line(s);
end;
/
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;
/
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;
/
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 := #
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;
/
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;
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;
/
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');