SELECT a.amount + ROUND(a.amount * b.RATE/100,4)
 into p_amount
 FROM test1 a,test2 b
 WHERE a.id = b.id ;如何设置游标b_amount:=0
b_amount=b_amount+p_amount循环去数

解决方案 »

  1.   

    游标可以这样写吗?
    SELECT a.amount + ROUND(a.amount * b.RATE/100,4) 
    into p_amount 
    FROM test1 a,test2 b 
    WHERE a.id = b.id ;如何设置游标 
     
     
      

  2.   

    b_amount=b_amount+p_amount       ==>    SUM(p_amount)So change the sql as following:
    SELECT sum(a.amount + ROUND(a.amount * b.RATE/100,4)) as p_amount 
      FROM test1 a,test2 b 
     WHERE a.id = b.id;
    By your querys, consult following Oracle function ff_test_l:
    create or replace function ff_test_1
    return varchar2
    is b_amount test1.amount%type;
    p_amount test1.amount%type;
    sqltext varchar2(2000);
    type mycurtype is ref cursor;
    cur_l mycurtype;
    begin
    sqltext := 'SELECT a.amount + ROUND(a.amount * b.RATE/100,4) as p_amount FROM test1 a,test2 b WHERE a.id = b.id';
    b_amount := 0;
    open cur_l for sqltext;
    loop
       fetch cur_l into p_amount;
       exit when cur_l%notfound;
       b_amount := b_amount + p_amount;
    end loop;return b_amount;
    end ff_test_1;
      

  3.   

    cursor c_amount  is
          SELECT a.amount + ROUND(a.amount * b.RATE/100,4) 
          as p_amount 
          FROM test1 a,test2 b 
          WHERE a.id = b.id ;
    r_amount c_amount%rowtype;  
    open c_amount;   --//打开游标    
    loop
               fetch c_amount into r_amount ;
               exit when c_amount%notfound;
       begin 
       b_amount :=b_amount+r_amount.p_amount;
    .......................
       exception
    ...........
       end;
    end loop;    
    close c_amount;    --//关闭