Is it possible to have multiple tables in one TRIGGER? Let say I have Employee, Skill and Customer Tables and I have Eventlogs table to capture the audit. I tried to add Skill_T but I got ORA-04079 error. Any correction? Thank you!
NOTE: I am using Oracle SQL Developer Oracle11gEE
CREATE OR REPLACE TRIGGER AUDIT_REC
AFTER INSERT OR DELETE OR UPDATE ON EMPLOYEE_T, SKILL_T
FOR EACH ROW
DECLARE
V_LOGID NUMBER;
V_USER VARCHAR(30);
V_DATE VARCHAR(30);
BEGIN
SELECT EVENTLOG_ID_SEQ.NEXTVAL, USER, SYSDATE INTO V_LOGID, V_USER, V_DATE FROM DUAL;
IF INSERTING THEN
INSERT INTO EVENTLOGS(Eventlog_id, User_name, Date_done, Action_done)
VALUES (V_LOGID, V_USER, V_DATE, 'INSERT');
ELSIF DELETING THEN
INSERT INTO EVENTLOGS(Eventlog_id, User_name, Date_done, Action_done)
VALUES (V_LOGID, V_USER, V_DATE, 'DELETE');
ELSIF UPDATING THEN
INSERT INTO EVENTLOGS(Eventlog_id, User_name, Date_done, Action_done)
VALUES (V_LOGID, V_USER, V_DATE, 'UPDATE');
END IF;
END;
/
A DML Trigger is associated (tied to) with only ONE table. It executes when DML is submitted against that table only.
See http://docs.oracle.com/cd/E11882_01/appdev.112/e25519/triggers.htm#LNPLS99888
Related
I have this set of data. I want to develop a trigger which fires when rows with ICICUT = IB are updated. So far it's proving a challenge because of error "PLS-00201: identifier 'OLD.ICICUT' must be declared". What am I missing?
ICICUT ICICU ICAME
IB 11368 65625
V 711340 63808
V 711313 24812
IB 711265 60238
O 711322 21570
RB 711370 348590
my trigger....
CREATE OR REPLACE TRIGGER EOGONY.F0011_audit
BEFORE UPDATE
ON INVOICES
REFERENCING NEW AS New OLD AS Old
FOR EACH ROW
WHEN (OLD.ICICUT = 'IB')
ENABLE
DECLARE
v_date varchar2(30);
BEGIN
SELECT TO_CHAR(sysdate, 'DD/MON/YYYY HH24:MI:SS') INTO v_date from dual;
INSERT INTO AUDIT_HISTORY_F0011 (ICICUT, ICICU, ICUSER, ICDICJ, OLD_ICAME, NEW_ICAME, ENTRY_DATE, OPERATION)
VALUES (:OLD.ICICUT, :OLD.ICICU , :NEW.ICUSER, :OLD.ICDICJ, :OLD.ICAME, :NEW.ICAME, v_date, 'Updating');
END;
I managed to compile code without errors after applying these changes...
CREATE OR REPLACE TRIGGER EOGONY.F0011_audit
BEFORE UPDATE
ON INVOICES
REFERENCING NEW AS New OLD AS Old
FOR EACH ROW
ENABLE
DECLARE
v_date varchar2(30);
BEGIN
IF ( :OLD.ICICUT = 'IB' ) THEN
SELECT TO_CHAR(sysdate, 'DD/MON/YYYY HH24:MI:SS') INTO v_date from dual;
INSERT INTO AUDIT_HISTORY_F0011 (ICICUT, ICICU, ICUSER, ICDICJ, OLD_ICAME, NEW_ICAME, ENTRY_DATE, OPERATION)
VALUES (:OLD.ICICUT, :OLD.ICICU , :NEW.ICUSER, :OLD.ICDICJ, :OLD.ICAME, :NEW.ICAME, v_date, 'Updating');
END IF;
END;
/
I am getting 0099 error while creating below trigger:
create trigger AuditTrigger1
before update, insert
on MPUZNTAB
for each row
declare
begin
insert into AuditTable1
(ZNCODE, DES , SHDES, WhenChanged)
values
(:new.ZNCODE,:new.DES,:new.SHDES, getdate())
end;
Please recomment changes
You missed a semicolon at the end of the insert statement
You should write before update OR insert
If getdate() is not a user defined function, you should use SYSDATE instead, to get the current time
I'm not sure the :new values are correct. Are they fields of the table MPUZNTAB?
This code should work:
Create Or Replace Trigger AuditTrigger1
Before Update Or Insert On MPUZNTAB
For Each Row
Declare
Begin
Insert Into AuditTable1
(ZNCODE,
DES,
SHDES,
WhenChanged)
Values
(:new.ZNCODE,
:new.DES,
:new.SHDES,
SYSDATE);
End;
I am not able to find the answer using search. How can I get the value for column FK_SOCRD_ID from table CR_MDT to insert into table CR_MDT_AUDIT using this trigger I have created:
CREATE OR REPLACE TRIGGER "CR_MDT_AUDIT"
before insert or delete or update on CR_MDT
for each row
declare
V_user varchar2 (30);
V_date varchar2 (30);
begin
IF inserting THEN
insert into CR_MDT_AUDIT (new_value, OLD_VALUE, user_name, entry_date, operation, FK_SOCRD_ID)
VALUES (:NEW.CR_MDT_ABSTRACTOR_DT, NULL, v_user, v_date, 'Abstraction Completed');
ELSIF DELETING THEN
insert into CR_MDT_AUDIT (new_value, OLD_VALUE, user_name, entry_date, operation, FK_SOCRD_ID)
VALUES (null, :OLD.CR_MDT_ABSTRACTOR_DT, v_user, v_date, 'delete');
ELSIF UPDATING THEN
insert into CR_MDT_AUDIT (new_value, OLD_VALUE, user_name, entry_date, operation, FK_SOCRD_ID)
VALUES (:NEW.CR_MDT_ABSTRACTOR_DT, :OLD.CR_MDT_ABSTRACTOR_DT, v_user, v_date, 'Update');
END IF;
END;
I don't know what clause or wording to use so that the FK_SOCRD_ID value from CR_MDT adds to CR_MDT_AUDIT table. Basically, I am trying to have a way to identify where the changes are being made.
DROP TRIGGER EPI_BOREHOLE_INI;
CREATE OR REPLACE TRIGGER EPI_BOREHOLE_INI
INSTEAD of Insert ON EPI_BOREHOLE for each row
DECLARE
V_ID number(10);
V_USER varchar2(100);
BEGIN
if (:new.UBHI is null or :new.NAME is null or) then
Raise_Application_Error(-20101, 'Insert failed. The key values of BOREHOLE(UBHI, NAME) cannot be null');
end if;
begin
select BOREHOLE_ID.nextval into V_ID from dual;
SELECT USER INTO V_USER FROM DUAL;
INSERT INTO EPI_BOREHOLE (ID, UBHI, NAME, INSERT_DATE ,INSERT_NAME, UPDATE_DATE,UPDATE_NAME) VALUES (V_ID,:NEW.UBHI, :NEW.NAME, SYSDATE, V_USER, SYSDATE, V_USER);
end;
END;
I see little error in your code: Please see below:
if (:new.UBHI is null or :new.NAME is null or) then <-- An additional OR written.
remove it and try or put one more condition.
I dont see any issue with the code.
Try doing
SET DEFINE OFF
Lets say I have a table as follows--
create table employees
(
eno number(4) not null primary key,
ename varchar2(30),
zip number(5) references zipcodes,
hdate date
);
And I'm trying to create a trigger with--
CREATE OR REPLACE TRIGGER TWELVE_ONE
BEFORE INSERT OR UPDATE
ON EMPLOYEES
FOR EACH ROW
DECLARE
V_DATE VARCHAR2 (10);
BEGIN
SELECT TO_CHAR (SYSDATE, 'hh24:mi:ss') INTO V_DATE FROM DUAL;
IF (V_DATE >= '12:00:01' AND V_DATE < '13:00:00')
THEN
INSERT INTO TABLE ?????
ELSE
ROLLBACK? TERMINATE TRANSACTION?
END IF;
END;
Purpose of the trigger is to allow an insertion/update during 12:00-13:00 and prevent the insertion at any other time. The trigger construction (thanks to #Melkikun) is seems ok. However now I'm facing the following issues--
How is it possible to pass the values here? I mean lets say my create statement is:
Insert into employees Values (1, 'someone', 11111, '17-12-2015')
And lets say the time is 12:30:01 now. How would the trigger perform the insertion without knowing the values?
And lets say the time is now 13:00:1 now. How would the trigger stop/prevent the insertion?
I'm using Oracle SQL Developer 4.02.15
Many Thanks
You just have to do it the other way.
If the time is not correct,then you raise an exception, so the insert won't be done.
CREATE OR REPLACE TRIGGER TWELVE_ONE
BEFORE INSERT OR UPDATE
ON EMPLOYEES
FOR EACH ROW
DECLARE
V_DATE VARCHAR2 (10);
MyException exception;
BEGIN
SELECT TO_CHAR (SYSDATE, 'hh24:mi:ss') INTO V_DATE FROM DUAL;
IF (V_DATE < '12:00:01' OR V_DATE > '13:00:00')
THEN
raise MyException;
END IF;
EXCEPTION
When MyException then
ROLLBACK;
//output message ...
END;
How would the trigger perform the insertion without knowing the values?
The trigger knows the value thanks to :NEW and :OLD.
You normally use the terms in a trigger using :old to reference the old value and :new to reference the new value.So you will have :NEW.eno ,:NEW.ename ...
Here is an example from the Oracle documentation :
CREATE OR REPLACE TRIGGER Print_salary_changes
BEFORE DELETE OR INSERT OR UPDATE ON Emp_tab
FOR EACH ROW
WHEN (new.Empno > 0)
DECLARE
sal_diff number;
BEGIN
sal_diff := :new.sal - :old.sal;
dbms_output.put('Old salary: ' || :old.sal);
dbms_output.put(' New salary: ' || :new.sal);
dbms_output.put_line(' Difference ' || sal_diff);
END;