问题
I have to write a trigger in db.
I wanted to declare some local variables inside it and do some manipulations before inserting the value to a table.
Using DB2, is it possible to have local variables inside trigger code?
回答1:
Yes you can!
Here's some example code (All in SQL) from the iSeries DB2 SQL Programming manual (which tends to run a few versions behind LUW):
CREATE TRIGGER TransactionBeforeTrigger BEFORE INSERT ON TransactionTable
REFERENCING NEW AS new_row
FOR EACH ROW MODE DB2ROW
BEGIN
DECLARE newmonth SMALLINT; -- Here's the decleration
SET newmonth = MONTH(new_row.DateOfTransaction);
IF newmonth < 4 THEN
SET new_row.FiscalQuarter=3;
ELSEIF newmonth < 7 THEN
SET new_row.FiscalQuarter=4;
ELSEIF newmonth < 10 THEN
SET new_row.FiscalQuarter=1;
ELSE
SET new_row.FiscalQuarter=2;
END
回答2:
It depends on the platform. For the z/OS platform, the answer is no you cannot. z/OS tends to be behind in features compared to other platforms.
来源:https://stackoverflow.com/questions/8385312/can-i-declare-variables-inside-a-trigger