5

I am writing a Oracle trigger. This trigger should automatically set the value of the column "productId" to be the oid of the row just inserted.

The trigger I wrote is:

create or replace trigger MyProduct_id_trg 
after insert on MyProduct
begin 
   update MyProduct set productId = inserted.oid where oid = inserted.oid;
end; 

However, this does not work.

Can someone help me with this?

Regards.

1 Answer 1

13

Looks like you are trying to use SQL Server syntax on an Oracle database! Try this:

create or replace trigger MyProduct_id_trg 
before insert on MyProduct
for each row
begin 
   :new.productId := :new.oid;
end; 

(Note: before not after, and with for each row.)

Sign up to request clarification or add additional context in comments.

5 Comments

Sorry. Forgot to mention productId is a varchar2 and oid is int. Do I need to do some type conversion?
@Kevin - There will be implicit conversion (which will give errors if the product ID is not a perfect number), But is is always recommended you convert it explicitly whenever required. Also, WHY is product ID varchar2 if you will always store a number there?
Hi. I got the error: ORA-04098: trigger 'MYPRODUCT_ID_TRG' is invalid and failed re-validation
In SQL Plus you can type "show errors trigger myproduct_id_trg" to see what the error is.
Thanks a lot. This is solved. I just need to use := rather than = for Oracle.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.