A:表内数据
ID(int) NUM(int) price(int)
ma0101 4 10
ma0101 5 13
ma0101 7 14
cz0403 4 10
cz0403 5 13
cz0403 7 14
dg0909 1 13
dg0909 2 15
dg0909 3 17
dg0909 10 45
B表
ID num1 price1
ma0101 1 1
cz0403 2 2
dg0909 1 2
写pl/sql实现以下结果
ID num1 price1 num2 price2 num3 price3 num4 price4 num5 price5
ma0101 1 1 4 10 5 13 7 14
cz0403 2 2 4 10 5 13 7 14
dg0909 1 2 1 13 2 15 3 17 10 45
代码:
declare
tid ab.id%type;
tnum ab.num%type;
tprice ab.price%type;
counter number(2);
str varchar2(1000);
newstr varchar2(1000);
cursor c1
is (select distinct id from ab );
cursor c2(abid ab.id%type)
is (select num,price from ab where id = abid);
begin
str := 'id ';
select max(count(id)) into counter from ab group by id;
for i in 1..counter
loop
select concat(str,'num') into str from dual;
select concat(str,i) into str from dual;
select concat(str,' price') into str from dual;
select concat(str,i) into str from dual;
select concat(str,' ') into str from dual;
end loop ;
dbms_output.put_line(str);
open c1;
loop
fetch c1 into tid;
exit when c1%notfound;
newstr:=tid;
open c2(abid => tid);
loop
fetch c2 into tnum,tprice;
exit when c2 %notfound;
select concat(newstr,' ') into newstr from dual;
select concat(newstr,tnum) into newstr from dual;
select concat(newstr,' ') into newstr from dual;
select concat(newstr,tprice) into newstr from dual;
end loop;
dbms_output.put_line(newstr);
newstr :='';
close c2;
end loop;
close c1;
end;
oracle pl/sql实例
最新推荐文章于 2024-08-27 15:37:37 发布