IF語句格式(僅僅限於DB2過程、觸發器、函數中使用)
IF (條件)
THEN 語句;
ELSEIF (條件)
THEN 語句;
ELSEIF (條件)
THEN 語句;
... ...
ELSE
語句;
END IF;
例子:
[sql]
BEGIN
--取出最大ID
SET NEW_ID=NULL;
-- (SELECT TOP 1 my_id from TEMPVOUCHERNO ORDER BY my_id DESC)
SET NEW_ID=(SELECT my_id FROM "PIGGY".TEMPVOUCHERNO FETCH FIRST 1 ROWS ONLY);
--如果未取出來
IF NEW_ID IS NULL
THEN SET NEW_ID='0000001';
ELSE
BEGIN
DECLARE NUM VARCHAR(7);
--取出編號+1 CONVERT(VARCHAR,(CONVERT(INTEGER,RIGHT(NEW_ID,7))+1));
--CAST((CAST(RIGHT(NEW_ID,7) as INTEGER)+1) as VARCHAR(7));
--SET NUM=CONVERT(VARCHAR(7),(CONVERT(INTEGER,RIGHT(NEW_ID,7))+1));
--SET NUM=REPLICATE('0',7-LEN(NUM))+NUM;
SET NEW_ID=NUM;
END;
END IF;
insert into "PIGGY".TEMPVOUCHERNO (my_id) values(NEW_ID);
END
作者 cuiran