Any experts in Oracle or SqlServer?
I,m trying to write a trigger, when a new department is added,
• Checks the uniqueness of the DNO
– If the value is unique then it adds the department to the
table
– Otherwise, copies the department info into an audit table
I already created 3 table emp, dept, audittable and stored procedure that checks the uniqueness of Dept no
Below is the code i wrote for trigger, it does checks the uniqueness of Deptno but doesn't copy it to audit table when wrong dept is inserted.
CREATE OR REPLACE TRIGGER trig_dept_new
BEFORE INSERT on DEPT
FOR EACH ROW
DECLARE dept_exist NUMBER;
BEGIN
SELECT Count(DEPTNO) INTO dept_exist
FROM DEPT
WHERE DEPT.DEPTNO = :NEW.DEPTNO;
IF dept_exist >= 1 THEN
INSERT INTO AUDITDEPT (DEPTNO, DNAME, LOC)
values (:NEW.DEPTNO, :NEW.DNAME, :NEW.LOC);
RAISE_APPLICATION_ERROR
(-20685, 'DEPT is not added, copied into audit table');
END IF;
END;
/
SQL> insert into DEPT (DEPTNO, DNAME, LOC) values (20,'SUPPORT','DALLAS');
insert into DEPT (DEPTNO, DNAME, LOC) values (20,'SUPPORT','DALLAS')
*
ERROR at line 1:
ORA-20685: DEPT is not added, copied into audit table
ORA-06512: at "A.TRIG_DEPT_NEW", line 11
ORA-04088: error during execution of trigger 'A.TRIG_DEPT_NEW'
Any help would be appreciated.