Search code examples
sqloracle-databaseplsqltriggersdbms-output

Oracle SQL trigger - DBMS_OUTPUT.PUT_LINE


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;

Solution

  • 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