我的存储过程:
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;后面的循环插入的写法是错误的,请各位高手教教正确的写法是怎样的?

解决方案 »

  1.   

    补充:
    从外面传入的参数a 和EDate 的值分别是:'<','2007-01-01'
      

  2.   

    这样不就可以了吗?insert into tablename1 select col1,col2....... from tablename2
      

  3.   

    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
    and to_char(I_DATE, ''yyyy-MM-dd'') '|| a || EDate ||'
    group by PRODUCT_NUMBER, a.INCEPT_ROOM, b.PLACE';
    end;
      

  4.   

    hongqi162:谢谢你的解答!按照你的方法是可以插入数据,但插入表的数据中有一列是从存储过程外传过来的参数,其它的列值是从表中取。像我上面写的数值“8”实际上是个参数,只是当时写的时候觉得方便所以先用“8”来带替了,如果有插入参数值要怎么写?
      

  5.   

    test
    str varchar(2000);
    str:= '
    insert into table_insert
    (USERID, PRODUCT_ID, ROOMID, PLACEID, AMOUNT)
    select :p1,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
    and to_char(I_DATE, ''yyyy-MM-dd'') '|| a || EDate ||'
    group by PRODUCT_NUMBER, a.INCEPT_ROOM, b.PLACE';execute immediate str using 变量;