can i declare variables inside a trigger?

為{幸葍}努か 提交于 2021-01-29 08:01:36

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!