Insert Current User into Table Column Using BEFORE INSERT Trigger SQL - oracle

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;

Related

Why trigger not fire when updating

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.

How I can create a trigger with a derived attribute?

I have a database of a video store, to ask the question that I have exposed, I thought about updating an age attribute that corresponds to the actors, but this doesn't make sense (as I show below in an example)
CREATE TABLE dobs ( dob date, age number(3));
insert into dobs values ((to_date('1999-10-04','yyyy-mm-dd')),NULL);
CREATE OR REPLACE FUNCTION get_age
(
fnacimiento date
)
return int
is edad int;
begin
select floor(months_between(sysdate,dob)/12) into edad
from dobs
where dob=fnacimiento;
return edad;
end get_age;
CREATE OR REPLACE TRIGGER agec before INSERT OR UPDATE ON dobs
FOR EACH ROW
BEGIN
:new.age := get_age(:new.dob);
END;
You don't need a function in order to update that column. Even no need to use a SELECT Statement. Just rearrange the trigger as :
CREATE OR REPLACE TRIGGER agec BEFORE INSERT OR UPDATE ON dobs
FOR EACH ROW
BEGIN
:new.age := floor(months_between(sysdate,:new.dob)/12);
END;
Trigger will be fired once at time of insert or update of record in the table so you will get the age of actor when insert/update is executed, not as of today.
So better solution is to create a view instead of trigger as follows:
Create or replace view dobs_vw
As
Select t.* floor(months_between(sysdate,t.dob)/12) as age_asof_today
From your_table t

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.

Edit a row you just inserted in the db

I got an example that better explains the situation. I have a table A
CREATE TABLE A( ID NUMBER, VAL NVARCHAR2(255) )
and I create a trigger that does an update on the row it's just inserted
CREATE OR REPLACE TRIGGER XXX
AFTER INSERT
ON A
FOR EACH ROW
DECLARE
BEGIN
UPDATE A SET VAL = 'LOL' WHERE ID = :NEW.ID;
END;
When I perform an insert
INSERT INTO A VALUES(1, 'XX')
I get
ORA-04091: table name is mutating, trigger/function may not see it
Is there a workaround?
You don't need an update, just assign the new value in a BEFORE trigger.
CREATE OR REPLACE TRIGGER XXX
BEFORE INSERT --<< You need a BEFORE trigger for this to work.
ON A
FOR EACH ROW
BEGIN
:new.val := 'LOL';
END;

oracle trigger select updated row

I'm trying to build a trigger which takes the specific updated row and insert it into another table, but I'm not able to build it.
This is what I was able to build:
CREATE OD REPLACE TRIGGER Z_ONUPDATELOGIN
AFTER UPDATE OF LAST_LOGGED_IN_DATE ON CMN_SEC_USERS csu
BEGIN
INSERT INTO Z_LOGIN (name, login_date)
select first_name||last_name,
last_logged_in_date
from cmn_sec_users usr
where usr.id=csu.id;
END;
When you deal with triggers you can use the :NEW and :OLD keyword to work with the new and old values of the row you are modifying. In your case try this:
CREATE OR REPLACE TRIGGER Z_ONUPDATELOGIN
AFTER UPDATE OF LAST_LOGGED_IN_DATE ON CMN_SEC_USERS
FOR EACH ROW
BEGIN
INSERT INTO Z_LOGIN (name, login_date)
VALUES (:NEW.first_name || :NEW.last_name,
:NEW.last_logged_in_date);
END;

Resources