1

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
4

1 回答 1

2

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.

于 2012-05-29T05:02:46.043 回答