Oracle SQL trigger on update of column - sql

I'm trying to create a trigger for my table which automatically adds a published date based on when a certain flag is set to 'Y'
I don't have much experience with creating triggers but so far this is what i've got
create or replace
TRIGGER ADD_CREATE_DT
after UPDATE of approved ON articles
for each row
BEGIN
:new.create_dt := sysdate where approved = 'Y';
END;
I get this error when updating the column
trigger 'USER.ADD_CREATE_DT' is invalid and failed re-validation
Any ideas?
Thanks

Use the WHEN clause:
create or replace
TRIGGER ADD_CREATE_DT
after UPDATE of approved ON articles
for each row
when (new.approved = 'Y')
BEGIN
:new.create_dt := sysdate;
END;
Or use IF:
create or replace
TRIGGER ADD_CREATE_DT
after UPDATE of approved ON articles
for each row
BEGIN
if :new.approved = 'Y' then
:new.create_dt := sysdate;
end if;
END;
In this case, WHEN is more appropriate and efficient.

create or replace
TRIGGER ADD_CREATE_DT
after UPDATE of approved ON articles
for each row
BEGIN
IF :NEW.approved = 'Y' THEN
:new.create_dt := sysdate;
END IF;
END;

I don't know What version of Oracle do you use?
In Oracle 10g I got the following error:
ORA-04084: cannot change NEW values for this trigger type
04084. 00000 - "cannot change NEW values for this trigger type"
*Cause: New trigger variables can only be changed in before row
insert or update triggers.
*Action: Change the trigger type or remove the variable reference.
It does not allow to change the field on AFTER triggers.

Related

how to fix this trigger error in PostgreSQL [duplicate]

This question already has answers here:
syntax Error in PostgreSQL when I try to create Trigger
(2 answers)
Closed last year.
I am getting a syntax error for my code which I can't understand why
am I missing something?
also, I read this I did not get my answer
syntax Error in PostgreSQL when I try to create Trigger
CREATE TRIGGER MyExampleName AFTER INSERT ON baskets
FOR EACH ROW BEGIN
UPDATE customers
SET customers.credit=customers.credit - NEW.amount
WHERE customers.id = NEW.customer_id;
END;
and tried it like this as well:
CREATE TRIGGER MyExampleName AFTER INSERT ON baskets
FOR EACH ROW AS $$ BEGIN
UPDATE customers
SET customers.credit=customers.credit - NEW.amount
WHERE customers.id = NEW.customer_id;
END;
$$ LANGUAGE plpgsql;
Error:
ERROR: syntax error at or near "BEGIN"
LINE 2: FOR EACH ROW BEGIN
^
SQL state: 42601
Character: 67
I'd say the first comment on your question pretty much covers it all. You cannot put the trigger code in the trigger body, you must first create a separate function and include the function call inside the trigger body.
This example comes directly from the Postgres docs:
-- 1. Create the function that does what you need
CREATE FUNCTION emp_stamp() RETURNS trigger AS $emp_stamp$
BEGIN
-- Check that empname and salary are given
IF NEW.empname IS NULL THEN
RAISE EXCEPTION 'empname cannot be null';
END IF;
IF NEW.salary IS NULL THEN
RAISE EXCEPTION '% cannot have null salary', NEW.empname;
END IF;
-- Who works for us when they must pay for it?
IF NEW.salary < 0 THEN
RAISE EXCEPTION '% cannot have a negative salary', NEW.empname;
END IF;
-- Remember who changed the payroll when
NEW.last_date := current_timestamp;
NEW.last_user := current_user;
RETURN NEW;
END;
$emp_stamp$ LANGUAGE plpgsql;
-- 2. Create the trigger with the 'EXECUTE FUNCTION function_name' part
-- replacing the actual function name from step 1.
CREATE TRIGGER emp_stamp BEFORE INSERT OR UPDATE ON emp
FOR EACH ROW EXECUTE FUNCTION emp_stamp();

Oracle 12c AFTER INSERT OR UPDATE TRIGGER

I Have a following trigger, which updates but and rolling back automatically, I'm not finding what is the cause, kindly help.
FYI: updating table UDF_DATA has a foreign key reference to CCEX.CUSTOMER triggering table.
CREATE OR REPLACE TRIGGER TR_CUSTOMER_PM
AFTER INSERT OR UPDATE ON CCEX.CUSTOMER FOR EACH ROW
DECLARE
i_subscriber_id Number :=3080;
user_xcep EXCEPTION;
PRAGMA EXCEPTION_INIT( user_xcep, -20001 );
pragma autonomous_transaction;
i_syscode ccex.customer.cust_system_code%type;
BEGIN
IF :new.cust_account_number like 'TID%' THEN
i_syscode:= :new.cust_system_code;
update udf_data set value = 'Term'
where subscriber_id = i_subscriber_id
and cust_system_code = i_syscode
and entity_id = '1488_OTA'
and udf_id = '3994_OTA'
and name = 'Primary Manager';
END IF;
EXCEPTION
when others then
raise user_xcep;
END;
/
Since the trigger is in autonomous_transaction mode, it should close the transaction.
Please add commit just before the exception line.

Capture log information and raise exception in Oracle Trigger

I have a requirement where the status of the column changes from x to y.
I want to capture which process changed the value from x to y in a log table
and raise an exception to roll back the update using Oracle BEFORE UPDATE trigger
Is this possible using pragma autonomous_transaction ?
Please help.
It's possible. I'm assuming that you know how to identify the "process that changed the value" which I'm guessing is coming from some column in v$session or a user-defined context or some similar source.
You'd normally define a procedure
CREATE OR REPLACE PROCEDURE log_error( <<parameters>> )
AS
PRAGMA autonomous_transaction;
BEGIN
insert into log_table ...
commit;
END;
and then call that procedure from a trigger that throws an exception
CREATE OR REPLACE TRIGGER trg_no_x_to_y
BEFORE UPDATE ON table_name
FOR EACH ROW
BEGIN
IF( :old.column = x and :new.column = y )
THEN
log_error;
raise_application_error( -20001, 'Cannot change column from x to y' );
END IF;
END;

Oracle SQL trigger results in compilation error

I have a problem with trigger not working. Here is my trigger code
create or replace trigger "ZIVOTINJE_T2"
BEFORE
insert or update on "ZIVOTINJE"
for each row
begin
IF new.cijena>10 THEN
:new.cijena:=9.9
ELSEIF new.cijena<0 THEN
:new.cijena:=0.1
END IF;
end;
When I try to insert entity in table ZIVOTINJE, I get this
ORA-04098: trigger 'DENISS.ZIVOTINJE_T2' is invalid and failed re-validation
I can see three problems in the code of your trigger:
You need to refer to the new values of the row using :new (including the colon), not new.
To change values that are about to be inserted/updated, write :new.cijena := 9.9; instead of SET new.cijena=9.9. Note that (a) there is no SET keyword here; (b) the assignment operator is :=, not =; and (c) you need a semi-colon at the end of the line.
Use ELSIF instead of ELSEIF.
Finally, in SQL*Plus, you can use SHOW ERRORS TRIGGER "ZIVOTINJE_T2" to show the errors for this trigger.
try this.
CREATE OR REPLACE TRIGGER "ZIVOTINJE_T2"
BEFORE
insert or update on "ZIVOTINJE"
for each row
begin
IF :new.cijena = 10 THEN
:new.cijena := 9.9;
ELSE
IF :new.cijena < 0 THEN
:new.cijena := 0.1;
END IF;
end if;
end;

Ora-04072: INVALID TRIGGER TYPE

I'm trying to execute the following SQL statement on Oracle 11g. I'm not experienced when it comes to Oracle and I'm not sure why this is failing. This query was provided to me by our developer.
I was attempting to execute this through the SQL worksheet in OEM.
CREATE OR REPLACE TRIGGER TBL_ADMINCOMMAND_TRG BEFORE
INSERT OR UPDATE ON tbl_AdminCommands FOR EACH ROW
BEGIN
IF inserting
AND :new.ADMINCOMMANDID IS NULL THEN
SELECT TBL_ADMINCOMMANDS_SEQ.nextval INTO :new.ADMINCOMMANDID FROM DUAL;
END IF;
END;
ALTER TRIGGER TBL_ADMINCOMMAND_TRG ENABLE;
The code you show works for me, but only as two separate commands:
1)
CREATE OR REPLACE TRIGGER TBL_ADMINCOMMAND_TRG BEFORE
INSERT OR UPDATE ON tbl_AdminCommands FOR EACH ROW
BEGIN
IF inserting
AND :new.ADMINCOMMANDID IS NULL THEN
SELECT TBL_ADMINCOMMANDS_SEQ.nextval INTO :new.ADMINCOMMANDID FROM DUAL;
END IF;
END;
2)
ALTER TRIGGER TBL_ADMINCOMMAND_TRG ENABLE;
Try doing them one at a time.
As an aside, this line:
SELECT TBL_ADMINCOMMANDS_SEQ.nextval INTO :new.ADMINCOMMANDID FROM DUAL;
can be simplified to this in 11G:
:new.ADMINCOMMANDID := TBL_ADMINCOMMANDS_SEQ.nextval;
In fact, the whole trigger can be simplified to:
CREATE OR REPLACE TRIGGER TBL_ADMINCOMMAND_TRG
BEFORE INSERT ON tbl_AdminCommands
FOR EACH ROW
WHEN (NEW.ADMINCOMMANDID IS NULL)
BEGIN
:new.ADMINCOMMANDID := TBL_ADMINCOMMANDS_SEQ.nextval;
END;
If you are using SQL*Plus, you should end your PL/SQL commands with a single forward slash on a line by itself:
CREATE OR REPLACE TRIGGER TBL_ADMINCOMMAND_TRG
BEFORE INSERT OR UPDATE ON tbl_AdminCommands
FOR EACH ROW
BEGIN
IF inserting AND :new.ADMINCOMMANDID IS NULL
THEN
SELECT TBL_ADMINCOMMANDS_SEQ.nextval
INTO :new.ADMINCOMMANDID
FROM DUAL;
END IF;
END;
/
ALTER TRIGGER TBL_ADMINCOMMAND_TRG ENABLE;
Also note that if your trigger uses IF inserting you could do only a trigger BEFORE INSERT.