DB2 IF 测试

--DB2 IF 测试
--彭建军
--2005-12-5 12:24下午

--建立测试环境
DROP TABLE T1;

CREATE TABLE T1
(
 C1 INT,
 C2 INT
);

INSERT INTO T1(C1) VALUES (1),(2),(3),(4),(99);

DROP PROCEDURE P1@

CREATE PROCEDURE P1
LANGUAGE SQL
BEGIN
  FOR v1 AS c1 CURSOR FOR
   SELECT C1 v_C1 FROM T1
  DO
   IF (v_C1 = 1) THEN
     UPDATE T1 SET C2 = v_C1 WHERE C1 = v_C1;
   ELSEIF (v_C1 = 2) THEN
    UPDATE T1 SET C2 = v_C1 WHERE C1 = v_C1;
   ELSEIF (v_C1 = 3) THEN
    UPDATE T1 SET C2 = v_C1 WHERE C1 = v_C1;
   ELSEIF (v_C1 = 4) THEN
     BEGIN
     UPDATE T1 SET C2 = v_C1 WHERE C1 = v_C1;
     UPDATE T1 SET C2 = v_C1 * 2 WHERE C1 = v_C1;
    END;
   ELSE
    UPDATE T1 SET C2 = 123456 WHERE C1 = v_C1;
    END IF;
  END FOR;
END
@

--查询语句
SELECT * FROM T1

--执行存储过程
CALL P1

--查询结果
SELECT * FROM T1

阅读更多

没有更多推荐了,返回首页