当前位置:数据库 > DB2 >>

DB2 CASE/IF 条件控制语句

 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
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,