Objective
Every morning a
newspaper vendor newspapers in wholesale from a distributor for 60 paise. He
sells them in retail for 75 paise. At the end of the day the unsold papers are
returned to the distributor for 30 paise rebate per paper. Write a PL/SQL code
block to prepare a report for the newspaper vendor in the following format with
5 weeks data.
Week Bought Sold Return Profit Loss
1 60 30
2 75 40
. . .
program:
SQL>Create table vendor (week number(2) primary key, bought number(2), sold
number(2));
Table created.
SQL>inser into vendor values(1,100,75);
1 row created.
SQL>insert into vendor values(2,70,60);
1 row created.
SQL>insert into vendor values(3,45,20);
1 row created.
SQL>insert into vendor values(4,60,55);
1 row created.
SQL>insert into vendor values(5,50,50);
1 row created.
SQL>ALTER TABLE vendor add(return number(10), profit number(6,2), loss
number(6,2));
Table altered.
SQL> ed news;
declare
v-pro vendor .profit
%type;
v-lo vendor .Loss
%type;
v-ret vendor .Return % type;
pr number (10);
cursor C is
select *
from vendor;
begin
for i in C loop
v-ret:=i.bought-i.Sold;
if (v-ret=o)then
v-pro:=(i.sold*.75)-(i.bought*.60);
v-lo:=0;
els if (v-ret>0)then
v-pro:=(i.sold
*.75)+(v-ret *.30)-(i.bought * .60);
if (v-pro
< 0) then
v-lo : = -1 *
v-pro;
v-pro : = 0;
else
v- lo : =0;
end if ;
end if ;
UPDATE vendor set return = v-re, profit = v-pro ,
loss = v-lo where
week = i.week ;
end loop;
end ;
SQL >
@news ;
20 /
PL/SQL procedure successfully completed .
SQL > Select *
from vendor ;
week Bought sold
Return profit Loss
1 100 75 25 3.75 0
2 70 60 10 6 0
3 45 20 25 0 4.5
4 60 55 5 6.75 0
5 50 50 0 7.5 0
0 comments:
Post a Comment