oracle如何循环的从一个表里取值,在存储过程中建一个动态游标后如何循环取游标指向的记录并插入另一个表中...

本文讨论了在Oracle存储过程中如何创建动态游标,并循环取出游标中的记录,然后将这些记录插入到另一个表中。示例代码展示了如何构建SQL查询字符串,打开游标,以及在循环中插入数据的错误方法。解决方案是使用EXECUTE IMMEDIATE直接执行动态SQL进行数据插入。
摘要由CSDN通过智能技术生成

当前位置:我的异常网» Oracle开发 » 在存储过程中建一个动态游标后如何循环取游标指向的

在存储过程中建一个动态游标后如何循环取游标指向的记录并插入另一个表中

www.myexceptions.net  网友分享于:2013-12-12  浏览:757次

在存储过程中建一个动态游标后怎么循环取游标指向的记录并插入另一个表中?

我的存储过程:

create   or   replace   procedure   test(a   in   string,   EDate   in   string)   is

type   cur_t   is   ref   cursor;

c_FACT_PATI   cur_t;

str                   varchar2(5000);

rst   xx.table_insert%rowtype;

begin

str   :=   '   select   PRODUCT_NUMBER, ';

str   :=   str   ||   '   a.INCEPT_ROOM   as   room_id, ';

str   :=   str   ||   '     b.PLACE, ';

str   :=   str   ||   '     sum(Amount)   as   NewSumAmount ';

str   :=   str   ||   '   from   table1   a, ';

str   :=   str   ||   '             table2           b, ';

str   :=   str   ||   '             table3           c ';

str   :=   str   ||   '   where   a.IN_ID   =   b.in_id ';

str   :=   str   ||   '     and   a.AFFIRM_SIGN   =   1 ';

str   :=   str   ||   '     and   b.NUMBER   =   c.id ';

str   :=   str   ||   '     and   to_char(I_DATE,   '   ||   ' ' 'yyyy-MM-dd ' ' '   ||   ') '   ||   a   ||   ' ' ' '   ||

EDate   ||   ' ' ' ';

str   :=   str   ||   '     group   by   PRODUCT_NUMBER,   a.INCEPT_ROOM,   b.PLACE ';

open   c_FACT_PATI   for   str;

loop

fetch   c_FACT_PATI   into   rst;

exit   when   c_FACT_PATI%notfound;

insert   into   table_insert

(USERID,   PRODUCT_ID,   ROOMID,   PLACEID,   AMOUNT)

values

(8,

rst.PRODUCT_NUMBER,

rst.room_id,

rst.PLACE,

rst.NewSumAmount);

end   loop;

close   c_FACT_PATI;

end   test;

后面的循环插入的写法是错误的,请各位高手教教正确的写法是怎样的?

------解决方案--------------------

create or replace procedure test(a in string, EDate in string) is

begin

execute immediate '

insert into table_insert

(USERID, PRODUCT_ID, ROOMID, PLACEID, AMOUNT)

select 8,PRODUCT_NUMBER,a.INCEPT_ROOM as room_id,b.PLACE,sum(Amount) as NewSumAmount

from table1 a,

table2 b,

table3 c

where a.IN_ID = b.in_id

and a.AFFIRM_SIGN = 1

and b.NUMBER = c.id

文章评论

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值