0

对于一个项目,我需要根据同一属性的值修改表中的一些属性。我决定使用游标,并写了以下内容:

DECLARE
totale_rente rekening.saldo%TYPE;
cursor c_boven1000 is
select r.reknummer,
       r.saldo,
       rt.rentepercentage,
       ABS(ROUND(r.saldo * (rt.rentepercentage/100), 2 )) as teBetalen
       FROM rekening r
       join rekeningtype rt on rt.naam = r.rekeningtype
       Where r.saldo < 0 and saldo >= -1000;
 cursor c_onder1000 is 
 select r.reknummer,
        r.saldo,
        rt.rentepercentage,
        ABS(ROUND(r.saldo * ((rt.rentepercentage*2)/100), 2 )) as teBetalen
        From rekening r
        join rekeningtype rt on rt.naam = r.rekeningtype
        Where r.saldo < -1000;

 TYPE rek_saldo IS TABLE OF rekening.saldo%TYPE;
 TYPE rek_nummer IS TABLE OF rekening.reknummer%TYPE;
 TYPE type_percentage IS TABLE OF rekeningtype.rentepercentage%TYPE;
 TYPE rek_tebetalen IS TABLE OF rekening.saldo%TYPE;

 rek_saldos rek_saldo;
 rek_nummers rek_nummer;
 type_percentages type_percentage;
 rek_tebetalens rek_tebetalen;

BEGIN
OPEN c_boven1000;
FETCH c_boven1000 BULK COLLECT INTO rek_saldos, rek_nummers, type_percentages, rek_tebetalens;
CLOSE c_boven1000;
FOR x IN rek_nummers.first..rek_nummers.last LOOP
    UPDATE rekening r
    SET r.saldo = r.saldo - rek_tebetalens(x)
    WHERE r.reknummer = rek_nummers(x);
    totale_rente := totale_rente + rek_tebetalens(x);
END LOOP;
OPEN c_onder1000;
FETCH c_onder1000 BULK COLLECT INTO rek_saldos, rek_nummers, type_percentages, rek_tebetalens;
CLOSE c_onder1000;
FOR x IN rek_nummers.first..rek_nummers.last LOOP
    UPDATE rekening r
    SET r.saldo = r.saldo - rek_tebetalens(x)
    WHERE r.reknummer = rek_nummers(x);
    totale_rente := totale_rente + rek_tebetalens(x);
END LOOP;    

    UPDATE rekening r
    SET saldo = saldo + totale_rente
    WHERE r.reknummer = '2250';
END;

在这种情况下,reknummer是一个 Varchar,saldo是一个数字(10,2),rentepercentage是一个数字(3,2)。

执行时,我收到以下错误:

ORA-01722: 执行 PL/SQL 代码的编号无效。

不确定它是否重要,但此代码块位于单击按钮的动态操作中。我试图找到我的错误,但一直无法找到。有人可以帮忙吗?

4

1 回答 1

3

您选择列的顺序和变量的顺序不同。

 select r.reknummer
       ,r.saldo
       ,rt.rentepercentage

 into rek_saldos
          ,rek_nummers
          ,type_percentages

您可以在光标中使用 case 语句,如下所示:

select r.reknummer
      ,r.saldo
      ,rt.rentepercentage
      ,case
          when saldo >= -1000 then
           abs(round(r.saldo * (rt.rentepercentage / 100), 2))
          else
           abs(round(r.saldo * ((rt.rentepercentage * 2) / 100), 2))
       end as tebetalen
  from rekening r
  join rekeningtype rt
    on rt.naam = r.rekeningtype
 where r.saldo < 0
于 2016-11-09T12:07:10.417 回答