Professional Documents
Culture Documents
PL Sqllab Cycles5it
PL Sqllab Cycles5it
PL Sqllab Cycles5it
1. Write a pgm to check whether a particular item code is present in item table or
not.If data is found then dispaly details of that item other wise dispaly message
'Item not found'
Program Code:
set serveroutput on
declare
item_rec item%rowtype;
item_id item.item_code%type;
begin
item_id:='&item_id';
select * into item_rec from item where item_code=item_id;
dbms_output.put_line(item_rec.item_code||' '||item_rec.item_name||'
'||item_rec.stock_num||' '||item_rec.unit_price);
EXCEPTION
when NO_DATA_FOUND THEN
dbms_output.put_line('iTEM DOES NOT EXISTS');
END;
/
PL/SQL PROGRAMS USING CURSORS
1.List the contents of the orders table.
2. Display item name,item code,stock number and unit price
from item table for a particular item code entered by user .(use
parameterized cursor)
program code
set serveroutput on
declare
Cursor cur(par varchar2)is select item_name ,stock_num,unit_price
From
item where item_code like par;
im item.item_name%type;
snum item.stock_num%type;
up item.unit_price%type;
ino1 item.item_code%type;
BEGIN
ino1:='&ino1';
Open cur(ino1);
Loop
Fetch cur into im,snum,up;
Exit when cur%notfound;
dbms_output.put_line(im||''||snum||''||up);
end loop;
close cur;
END;
/
2. List the itemno and the description of item having
unit_price more than 100.
Program code
set serveroutput on
declare
Cursor cur is select item_code,item_name
,stock_num,unit_price
From
cur_rec item%rowtype;
BEGIN
Open cur;
Loop
Fetch cur into cur_rec;
Exit when cur%notfound;
dbms_output.put_line(cur_rec.item_code||' '||
cur_rec.item_name||' '||cur_rec.stock_num||' '||
cur_rec.unit_price);
end loop;
close cur;
END;
/
3. List the details of the client in the city of the Bombay.
Program Code
set serveroutput on
declare
Cursor cur is select cust_id,cust_name,hs_name,city
From
cid varchar2(6);
cname varchar2(6);
hsname varchar2(6);
cit varchar2(6);
BEGIN
Open cur;
Loop
Fetch cur into cid,cname,hsname,cit;
Exit when cur%notfound;
close cur;
4. Display the itemcode, qty_on_hand and unit price of the first
five products having highest unit_price
PROGRAM CODE
set serveroutput on
declare
Cursor cur is select
item_code,item_name,stock_num,unit_price
From
item order by unit_price desc;
crec item%rowtype;
BEGIN
Open cur;
dbms_output.put_line(crec.item_code||' '||
crec.item_name||' '||crec.stock_num||' '||crec.unit_price);
end loop;
close cur;
END;
/
2. Write a PL/SQL program using cursor to check whether the given customer id is
in the given range.If not , write a user defined exception to handle it.
Program Code
set serveroutput on
declare
cid customer.cust_id%type;
ctid customer.cust_id%type;
f number;
NOT_IN_RANGE EXCEPTION;
cursor ccur is select cust_id from customer where cust_id
in('c0001','c0002','c0003','c0004');
begin
f:=0;
ctid:='&ctid';
open ccur;
loop
fetch ccur into cid;
exit when ccur%notfound;
if ctid = cid then
dbms_output.put_line('found in range');
exit;
else
RAISE NOT_IN_RANGE;
END IF;
end loop;
close ccur;
EXCEPTION
when NOT_IN_RANGE then
dbms_output.put_line(' ENTERED iTEM IS NOT IN THE GIVEN RANGE ');
END;
/
1.Write a procedure
1. Write a procedure to find minimum and maximum unit
price .Display the value in the main program.
Program #3
create or replace procedure maxmin62(max1 out
number,min1 out number) is
cursor cur is select unit_price from item;
temp number(8,2);
begin
open cur;
fetch cur into temp;
min1:=temp;
max1:=temp;
while cur%found loop
fetch cur into temp;
if temp>max1 then
max1:=temp;
end if;
if temp<min1 then
min1:=temp;
end if;
end loop;
end;
declare
mx number(8,2);
mn number(8,2);
begin
maxmin62(mx,mn);
dbms_output.put_line('Maximum is '||mx);
dbms_output.put_line('Minimun is '||mn);
end;
TRIGGER
1. Create a trigger on insertion in Customer table so that the
values inserted into the hs_name field must always be
Uppercase
SQL> create or replace trigger instrigger before insert on customer for each
row
2 begin
3 :new.hs_name := UPPER(:new.hs_name);
4 end;
5 /