oracle pl/sql实例

本文介绍了一个使用PL/SQL实现的两表连接案例,通过遍历查询两张表中的数据,并将结果按指定格式输出。该案例展示了如何在PL/SQL中使用游标进行数据操作,以及字符串拼接的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值