Ora 01403在尝试将数据插入oracle表时

时间:2014-12-18 23:33:32

标签: oracle plsql

我有以下触发器

        CREATE OR REPLACE TRIGGER L_BIUR_G_LAY
         BEFORE INSERT OR UPDATE ON G_LAY
         FOR EACH ROW
        When (new.g_roo is not null)
          DECLARE
          x number(1);
         stmt varchar(255);
           BEGIN
          FOR I IN (SELECT DISTINCT G_TAB FROM G_LEG)
          LOOP

               stmt := 'select distinct g_lind from ' || i.g_tab || ' where g_roo = ' || :new.g_roo;
          Execute immediate stmt into x;

        IF (x<>0)  THEN 
      RAISE_APPLICATION_ERROR(-2001, 'G_ROO  cannot be inserted where G_LIND IS NOT ZERO');
         END IF;
           END LOOP;
    END;
 /

当我做的时候

   insert into G_LAY (G_OGCS, G_OGC, G_ROO, G_NM, G_TI,   G_AB, G_DATE)
        (select G_LAY_SEQ.NEXTVAL, 1, G_ROO, G_LGNDIT, G_UNE, 'Pipe  Data Long - ' || G_UR, sysdate
             from G_DTABLE where G_LIND = 0);

我收到以下错误 错误报告: SQL错误:ORA-01403:找不到数据 ORA-06512:在&#34; L_BIUR_G_LAY&#34;,第10行 ORA-04088:执行触发器时出错&#39; G_LAY&#39; 01403. 00000 - &#34;未找到数据&#34;

非常感谢任何帮助。它在我的触发器中声明之后尝试了PRAGMA AUTONOMOUS_TRANSACTION,但它没有帮助我

1 个答案:

答案 0 :(得分:0)

问题不在于您的INSERT语句。执行触发器中动态生成的SELECT语句时会出现问题 - 没有找到数据,因此引发了ORA-01403。在我看来,最简单的事情就是做一个SELECT COUNT(...):

CREATE OR REPLACE TRIGGER L_BIUR_G_LAY
  BEFORE INSERT OR UPDATE ON G_LAY
  FOR EACH ROW
  When (new.g_rowno is not null)
DECLARE
  x number;
  stmt varchar(255);
BEGIN
  FOR I IN (SELECT DISTINCT G_DISPCNTRLTAB FROM G_LEG) LOOP
    stmt := 'select COUNT(*) from ' || i.g_dispcntrltab ||
            ' where g_rowno = ' || :new.g_rowno ||
              ' and g_lind <> 0';

    Execute immediate stmt into x;

    IF x <> 0 THEN 
      RAISE_APPLICATION_ERROR(-20001, 'G_RWNO  cannot be inserted where G_LIND IS NOT ZERO');
    END IF;
  END LOOP;
END L_BIUR_G_LAY;

我还更改了引发的错误号,因为-2001不在可以使用RAISE_APPLICATION_ERROR引发的错误范围内(有效范围是-20000到-20999)。

分享并享受。