PL Sqllab Cycles5it

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

PL/SQL PROGRAMS USING EXCEPTIONS

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

item where unit_price>80;

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

customer where city like 'bombay';

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;

dbms_output.put_line(cid||' '||cname||' '||hsname||' '||cit);


end loop;

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;

for i in 1..5 loop


Fetch cur into crec;

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;
/

PL/SQL PROGRAMS USING procedures

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; 

2. Write  procedure to find the details of  orders placed by


customers  whose name  contain ‘a’ as the second letter.
 
Program #5 

create or replace procedure details62 is cursor mycur is select


b.order_no,b.order_date,b.client_no,b.dely_type,b.billed_yn,b.d
ely_date,b.order_status from client_master62 a,sales_order62 b
where a.name like '_a%' and a.client_no=b.client_no;
begin
for rec in mycur loop
dbms_output.put_line(rec.order_no ||' '||rec.order_date||' '||
rec.client_no||' '||rec.dely_type||' '||rec.billed_yn||' '||
rec.dely_date||' '||rec.order_status);
end loop;
end;
begin
dbms_output.put_line('ORDER_NO      ORDER_DATE      
CLIENT_NO       DELY_TYPE      
BILLED_YN          BILLED_DATE       ORDER_STATUS');
details62;
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 /

Create a trigger to update stock number of the item.Updation is


possible only if the new stock number is greater than 500.else it
should give an error message.
SQL> create or replace trigger uptigger before update on item for each row
2 begin
3 if :new.stock_num<500 then
4 raise_application_error(-20011,'New % should be greater tha 500');
5 end if;
6 end;
7 /

You might also like