Oracle SQL trigger - DBMS_OUTPUT.PUT_LINE

Joe Doe picture Joe Doe · Jan 27, 2015 · Viewed 7.4k times · Source

I'm creating a trigger within my database, I came across two error that I am not able to fix, I'm pretty sure that those two are relating to my use of DBMS_OUTPUT.PUT_LINE, the rest of the statement does not cause any errors, although it had before.

Errors:

Error(5,3): PL/SQL: SQL Statement ignored

Error(5,15): PL/SQL: ORA-00903: invalid table name

Code:

CREATE TRIGGER INVOICES
BEFORE INSERT OR UPDATE ON BRUINVOICE
FOR EACH ROW 
BEGIN  
  IF :new.BRU_DATE < :new.BRU_PAID_DATE THEN
  DBMS_OUTPUT.PUT_LINE('You cannot do that');
  ELSE
  INSERT INTO table BRUINVOICE
  values 
  from inserted;
END IF;
END;

Answer

cstotzer picture cstotzer · Jan 27, 2015

Check constraints are a better choice (performance-wise) than triggers when it comes to record level validation:

ALTER TABLE bruinvoice
ADD CONSTRAINT validate_bru_date CHECK (BRU_DATE < BRU_PAID_DATE);

Inserting invalid data will raise an error message like the following:

scott@ORCL> insert into bruinvoice values ('21-DEC-14','20-DEC-14');
insert into bruinvoice values ('21-DEC-14','20-DEC-14')
*
ERROR at line 1:
ORA-02290: check constraint (SCOTT.VALIDATE_BRU_DATE) violated