线路编号如何在Oracle触发器中工作?

时间:2010-06-08 16:54:36

标签: oracle triggers

我有一个触发错误的触发器,我不知道如何知道哪一行PL / SQL代码会抛出该错误。我的错误是

  

[Oracle] ORA-01403:未找到任何数据   ORA-06512:at   “MYSCHEMA.FOO_BI”,第9行

我的触发器是这样的:

create or replace TRIGGER "MYSCHEMA"."FOO_BI" 
BEFORE INSERT ON FOO REFERENCING OLD AS OLD NEW AS NEW FOR EACH ROW
DECLARE
 NUM1 NUMBER;
 NUM2 NUMBER;

BEGIN

     -- some comment
  if :new.batch_num is null then
   SELECT COUNT(*) INTO :NEW.BATCH_NUM FROM FOO WHERE CORP_ID = :NEW.CORP_ID;
  end if;

  if :new.batch_id is null or :new.batch_id = '' then
    :NEW.BATCH_ID := :NEW.CORP_ID || '-' || :NEW.BATCH_NUM;
  end if;

/* etc... */     

我发现看起来像similar question,但行编号从create or replace...开始,并将我的错误行表示为评论,我认为必须是假的。在执行触发器时抛出错误时,如何报告行号?

2 个答案:

答案 0 :(得分:14)

行编号(在堆栈跟踪中报告)以DECLARE为第1行开始。因此,如果您执行以下操作:

CREATE OR REPLACE TRIGGER foo
BEFORE INSERT ON test1 
REFERENCING OLD AS OLD NEW AS NEW 
FOR EACH ROW
DECLARE
n1 NUMBER := 1;
n2 NUMBER := 2;

BEGIN

  -- a comment
  IF :new.n1 IS NULL THEN
    n1 := n2/0;
  END IF;
END;
/

SQL> insert into test1 values (3,'XX','YY',NULL);

insert into test1 values (3,'XX','YY',NULL)

ORA-01476: divisor is equal to zero
ORA-06512: at "XXX.FOO", line 9
ORA-04088: error during execution of trigger 'XXX.FOO'

SQL> select line, text from all_source where name = 'FOO';

      LINE TEXT
---------- --------------------------------------------------------------------------------
         1 TRIGGER foo
         2 BEFORE INSERT ON test1
         3 REFERENCING OLD AS OLD NEW AS NEW
         4 FOR EACH ROW
         5 DECLARE
         6 n1 NUMBER := 1;
         7 n2 NUMBER := 2;
         8 
         9 BEGIN
        10 
        11   -- a comment
        12   IF :new.n1 IS NULL THEN
        13     n1 := n2/0;
        14   END IF;
        15 END;

15 rows selected

您可以看到错误报告为第9行,实际上是第13行。

答案 1 :(得分:6)

行号是指Oracle数据字典中存储的源。您可以通过检查数据字典视图来确定实际的行编号。

SELECT text
  FROM all_source
  WHERE owner = 'MYSCHEMA'
    AND name = 'FOO_BI'
    AND type = 'TRIGGER'
    AND line = 9;