Why trigger not fire when updating - oracle

I developed a trigger to insert a history data for transaction table.
Create or replace trigger his_trg
After insert or update
On trans_tb
For each row
Declare
V_data number;
V_seq number := seq_name.nextval;
Begin
Select data into v_data from another_tb where key = :new.key;
If (inserting or updating) and nvl(:old.id) != (:New.id)
Insert into his_tb (column1, column2,column3,column4) Values (v_seq,v_data, :new.data1, :new.data2);
End if;
Exception
When others then
Dbms_output.put_line('error');
End;
Now issue is when i insert new record in trans_tb and update on newly inserted record means, records are inserted in his_tb.
But when i update already inserted record on trans_tb there will no insert happen in his_tb
Note: Am created trigger for trans_tb after 5 records are already created in trans_tb. And i made update on those 5 records will cause issue.

Related

Insert Current User into Table Column Using BEFORE INSERT Trigger SQL

I want to insert the current user into the "created_by" column using a before insert trigger.
Here's what I have so far:
create or replace trigger course_fp_trg
before insert on course
begin
:new.created_by := user;
end;
But I get the error "NEW or OLD references are not allowed in table level triggers"
How can I insert the current user into the "created_by" column of this table using a before insert trigger?
Try to make it a row level trigger by adding FOR EACH ROW.
create or replace trigger course_fp_trg
before insert on course for each row
begin
:new.created_by := user;
end;
You can try this one.
create or replace trigger course_fp_trg
before insert on course
referencing old as old new as new
for each row
begin
:new.created_by := user;
end;

How can I get the inserted primary key value from AFTER INSERT trigger in Oracle?

My Oracle DB has a table DOC_WF_COMM and its primary key is DWFC_ID. Primary key value is based on a sequence called SQ_DOC_WF_COMM.
I have created a row level AFTER INSERT trigger on that table and inside the trigger I need to join the inserted record with some other tables like this:
create or replace TRIGGER TRG_DOC_WF_COMM_AFT_INS AFTER INSERT ON DOC_WF_COMM REFERENCING OLD AS OLD NEW AS NEW FOR EACH ROW
DECLARE
PRAGMA AUTONOMOUS_TRANSACTION;
L_SUBJECT VARCHAR2(300);
L_BODY CLOB;
L_PNT_CODE VARCHAR(100) := NULL;
L_DR_PRJ_ID NUMBER(12);
L_STR_EMAIL VARCHAR2(120);
L_DWFC_TO_USR_ID VARCHAR2(12);
L_PNT_ID NUMBER(12);
L_PNT_EMAIL_YN VARCHAR(1);
L_PNT_ACTIVE_YN VARCHAR(1);
L_PNT_NOTIFY_YN VARCHAR(1);
BEGIN
IF INSERTING THEN
L_PNT_CODE := 'WFNT_MESSAGE';
SELECT DR_PRJ_ID, STR_EMAIL, DWFC_TO_USR_ID INTO L_DR_PRJ_ID, L_STR_EMAIL, L_DWFC_TO_USR_ID
FROM DOC_WF_COMM
JOIN DOC_WF_USERS ON DWFU_ID = DWFC_DWFU_ID
JOIN DOC_WORKFLOW ON DWF_ID = DWFU_DWF_ID
JOIN DOCUMENT_REF ON DR_ID = DWF_DR_ID
JOIN ST_REGISTER ON STR_ID = DWFU_STR_ID
WHERE DWFC_ID = :NEW.DWFC_ID AND DWFC_RESPONSE IS NULL;
-- SOME QUERIES HERE
END IF;
END;
The trigger is compiled successfully and when I insert record into DOC_WF_COMM table I get this error:
ORA-01403: no data found ORA-06512
The error is :NEW.DWFC_ID in WHERE clause and I have change it to these values:
:OLD.DWFC_ID
SQ_DOC_WF_COMM.NEXTVAL
SQ_DOC_WF_COMM.CURRVAL
But no any luck. Any idea why this error is and how can I resolve it?
The problem is this line in your trigger:
PRAGMA AUTONOMOUS_TRANSACTION;
That means the trigger executes as an isolated transaction in a separate session, which means it cannot see the uncommitted state of any other session. Crucially this includes the session which fires the trigger, so the autonomous transaction cannot see the record you just inserted. Hence, NO_DATA_FOUND.
You haven't posted the whole trigger or explained what you're trying to do, so only you know why you have included the PRAGMA. However, the chances are you don't need it. Remove the PRAGMA (and the COMMIT) and your trigger should work just fine.
If I understood you correctly, create a local variable and put the next sequence value in there. Then it can be referenced throughout the code, always having the same value. Something like this:
declare
l_seq number := my_seq.nextval;
begin
insert into table_a (id, ...) values (l_seq, ...);
update table_b set id = l_seq where ...
select ... into ... from ... where id = l_seq;
end;
I changed the query inside the trigger to this, it is working fine
SELECT STR_PRJ_ID, STR_EMAIL, :NEW.DWFC_TO_USR_ID INTO L_DR_PRJ_ID, L_STR_EMAIL, L_DWFC_TO_USR_ID
FROM DOC_WF_USERS, ST_REGISTER
WHERE :NEW.DWFC_TO_USR_ID = DWFU_US_ID AND DWFU_STR_ID = STR_ID AND DWFU_ID = :NEW.DWFC_DWFU_ID;
Not sure why is that. If anyone can figure out the mistake in the query given in the question, please let me know. Thanks

Oracle sql trigger to update table based on update of another field in the same table

I would like to create a trigger that updates a field in a table after an update of a different field in the same table. I don't think this is possible with an 'after insert or update' trigger. It compiles but when I update the field it mutates.
I have created a 'before insert or update' trigger which works for inserts, but doesn't work for updates.
This is my code:
create or replace
trigger TRIGGER_1
after insert or update of TOTAL_COUNT on TABLE_1
for each row
when (new.TOTAL_COUNT = 0)
begin
update TABLE_1
set count_accuracy_cde = 'absent';
end;
create or replace
trigger TRIGGER_1
before insert or update on test_table
referencing new as new old as old
for each row
begin
if (:new.TOTAL_COUNT = 0) then
:new.count_accuracy_cde := 'absent';
end if;
end;

Get the inserted row via trigger Oracle

I have a trigger that gets a sequence number to put into my id column when insert a Row, the question is, how easily retun the new inserted row as resultSet.
create or replace trigger trg_Dependencia_id
before insert on DEPENDENCIA
for each row
begin
select DEPENDENCIA_id_seq.nextval
into :new.id
from dual;
end;
In Oracle, Triggers does not return any value. Also, triggers are not for this issues.
Maybe, you can insert your nextval a temp table after insert your table(DEPENDENCIA.DEPENDENCIA_id). Then you can handle it with a function.

PL/SQL ORACLE triggers

I'm trying to create a trigger that when you insert an employee in the company, assign a commission of 0.10 if it is from department 80. This is what I've tried so far:
CREATE OR REPLACE TRIGGER emp_com
BEFORE INSERT ON employees
FOR EACH ROW
WHEN (NEW.DEPARTMENT_ID= '80')
BEGIN
IF INSERTING THEN
:NEW.commission_pct := 0.10;
END IF;
END;
Your trigger should work fine in its current state; however, the "IF INSERTING" bit is redundant since your trigger only fires for inserts anyway.
CREATE OR REPLACE TRIGGER emp_com
BEFORE INSERT ON employees
FOR EACH ROW
WHEN (NEW.DEPARTMENT_ID= '80')
BEGIN
:NEW.commission_pct := 0.10;
END;

Resources