DB2 CASE/IF 条件控制语句
来源:网络 责任编辑:栏目编辑 发表时间:2013-07-01 22:25 点击:次
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
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
相关新闻>>
最新推荐更多>>>
- 发表评论
-
- 最新评论 更多>>