mysql 游标

    自己案例  
    DELIMITER **  
    DROP PROCEDURE IF EXISTS SP_T6136 **  
    CREATE PROCEDURE SP_T6136 ()  
    BEGIN  
    -- 需要定义接收游标数据的变量   
         declare _f03 enum('BUY','SYS');    
         declare _f01 bigint(20);  
         declare _f02 tinyint(4) ;  
         declare _f04 datetime;  
         -- 判断游标到达最后的情况 该变量会在游标到达最后是变为1  
         DECLARE _STOP INT DEFAULT 0;  
         -- 声明游标  
         DECLARE _t6136 CURSOR FOR SELECT F01,f02,f03,f04 FROM S61.T6136 ;    
         -- 设置游标的终止条件  
         DECLARE CONTINUE HANDLER FOR NOT FOUND SET _STOP=1;  
         -- 打开游标  
         OPEN _t6136;     
      -- 提取游标里的数据  
     FETCH _t6136 INTO _f01,_f02,_f03,_f04;  
     /* 开始循环 */  
     WHILE _STOP<>1 DO   
        /*任务业务*/  
       select _f01 as a,_f02 as b,_f03 as e,_f04 as c;  
       /*游标向下走一步*/   
       FETCH _t6136 INTO _f01,_f02,_f03,_f04;  
          /* 结束循环 */  
         END WHILE;  
         -- 关闭游标  
         CLOSE _t6136;  
    END **  
    DELIMITER ;  
    CALL SP_T6136;  
      
      
    DELIMITER **  
    DROP PROCEDURE IF EXISTS SP_T6136 **  
    CREATE PROCEDURE SP_T6136 ()  
    BEGIN  
    -- 需要定义接收游标数据的变量   
         declare _f03 enum('BUY','SYS');    
         declare _f01 bigint(20);  
         declare _f02 tinyint(4) ;  
         declare _f04 datetime;  
         -- 判断游标到达最后的情况 该变量会在游标到达最后是变为True  
         DECLARE done INT DEFAULT true;  
         -- 声明游标  
         DECLARE _t6136 CURSOR FOR SELECT F01,f02,f03,f04 FROM S61.T6136 ;    
         -- 设置游标的终止条件  
         DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=false;  
          -- 打开游标  
         OPEN _t6136;     
      -- 提取游标里的数据  
     FETCH _t6136 INTO _f01,_f02,_f03,_f04;  
     /* 开始循环 */  
     WHILE done DO  
       /*任务业务*/  
       select _f01 as a,_f02 as b,_f03 as e,_f04 as c;  
       /*游标向下走一步*/   
       FETCH _t6136 INTO _f01,_f02,_f03,_f04;  
          /* 结束循环 */  
         END WHILE;  
         -- 关闭游标  
         CLOSE _t6136;  
    END **  
    DELIMITER ;  
    CALL SP_T6136;  
      
      
    DELIMITER **  
    DROP PROCEDURE IF EXISTS SP_T6136 **  
    CREATE PROCEDURE SP_T6136 ()  
    BEGIN  
    -- 需要定义接收游标数据的变量   
         DECLARE _f03 ENUM('BUY','SYS');    
         DECLARE _f01 BIGINT(20);  
         DECLARE _f02 TINYINT(4) ;  
         DECLARE _f04 DATETIME;  
         -- 判断游标到达最后的情况 该变量会在游标到达最后是变为True  
         DECLARE done INT DEFAULT FALSE;  
         -- 声明游标  
         DECLARE _t6136 CURSOR FOR SELECT F01,f02,f03,f04 FROM S61.T6136 ;    
         -- 设置游标的终止条件  
         DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=TRUE;  
          -- 打开游标  
         OPEN _t6136;   
           -- 开始循环  
            read_loop: LOOP  
       -- 提取游标里的数据  
      FETCH _t6136 INTO _f01,_f02,_f03,_f04;  
      /*判断是否结束循环*/  
      IF done THEN  
          LEAVE read_loop;  
      END IF;     
      /*任务业务*/  
      select _f01 as a,_f02 as b,_f03 as e,_f04 as c;  
            -- 关闭循环  
     END LOOP;  
         -- 关闭游标  
         CLOSE _t6136;  
    END **  
    DELIMITER ;  
    CALL SP_T6136;  
      
      
    DELIMITER **  
    DROP PROCEDURE IF EXISTS SP_T6136 **  
    CREATE PROCEDURE SP_T6136 ()  
    BEGIN  
    -- 需要定义接收游标数据的变量   
         DECLARE _f03 ENUM('BUY','SYS');    
         DECLARE _f01 BIGINT(20);  
         DECLARE _f02 TINYINT(4) ;  
         DECLARE _f04 DATETIME;  
         -- 判断游标到达最后的情况 该变量会在游标到达最后是变为True  
         DECLARE _STOP INT DEFAULT 1;  
         -- 声明游标  
         DECLARE _t6136 CURSOR FOR SELECT F01,f02,f03,f04 FROM S61.T6136 ;    
         -- 设置游标的终止条件  
         DECLARE CONTINUE HANDLER FOR NOT FOUND SET _STOP=0;  
          -- 打开游标  
         OPEN _t6136;   
           -- 开始循环  
            read_loop: LOOP  
       -- 提取游标里的数据  
      FETCH _t6136 INTO _f01,_f02,_f03,_f04;  
      /*判断是否结束循环*/  
      IF _STOP<>1 THEN  
          LEAVE read_loop;  
      END IF;     
      /*任务业务*/  
      select _f01 as a,_f02 as b,_f03 as e,_f04 as c;  
            -- 关闭循环  
     END LOOP;  
         -- 关闭游标  
         CLOSE _t6136;  
    END **  
    DELIMITER ;  
    CALL SP_T6136; 

 

 
posted @ 2015-03-27 14:13  匹夫鹏  阅读(173)  评论(0编辑  收藏  举报