Reputation: 4566
I want to make a trigger that sets the joined attribute to the current year. This is what I have working:
CREATE OR REPLACE TRIGGER foo2
BEFORE INSERT ON memberof
FOR EACH ROW
BEGIN :new.joined := 2012;
END;
I want to change 2012 to the code below but keep getting compiler errors. What's the proper syntax to accomplish this?
select extract(year from sysdate) from dual
Upvotes: 1
Views: 1295
Reputation: 81862
This should do the trick:
CREATE OR REPLACE TRIGGER foo2
BEFORE INSERT ON memberof
FOR EACH ROW
BEGIN
:new.joined := to_char(sysdate,'YYYY');
END;
For more information see here: http://docs.oracle.com/cd/B19306_01/server.102/b14200/sql_elements004.htm
and here: http://docs.oracle.com/cd/B19306_01/server.102/b14200/functions183.htm
BTW OMG Ponies is correct this normally should be done with default values which can take an expression like
to_char(sysdate,'YYYY')
as well.
Upvotes: 2