Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does line numbering work in an Oracle trigger?

I have a trigger that's throwing an error, and I am not sure how to know which line of PL/SQL code is throwing that error. My error is

[Oracle]ORA-01403: no data found ORA-06512: at "MYSCHEMA.FOO_BI", line 9

My trigger is something like this:

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... */     

I found what looks like a similar question but the line numbering starts with the create or replace... and represents my error line as a comment, which I think must be bogus. How is the line numbering reported when an error is thrown in execution of a trigger?

like image 734
Chris Farmer Avatar asked Jun 08 '10 16:06

Chris Farmer


2 Answers

The line numbering (as reported in stack traces) starts with the DECLARE being line 1. So, if you do the following:

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

You can see the error was reported as happening at line 9, which is actually line 13 in the source.

like image 161
DCookie Avatar answered Oct 21 '22 23:10

DCookie


The line numbers refer to the stored source in the Oracle data dictionary. You can determine the actual line numbering by checking the data dictionary views.

SELECT text
  FROM all_source
  WHERE owner = 'MYSCHEMA'
    AND name = 'FOO_BI'
    AND type = 'TRIGGER'
    AND line = 9;
like image 35
Dave Costa Avatar answered Oct 22 '22 00:10

Dave Costa