zoukankan      html  css  js  c++  java
  • mysql中游标的使用

    BEGIN
    #遍历占领野地表还原军队
    #update armforce set number=num where troopsid=(select id from troops where isfrist=1 and cityid=cityid)
        DECLARE tId DECIMAL(21,0) default -1;#occupyWild表的TroopsId  
            DECLARE cid DECIMAL(21,0) default -1;#occupyWild表的CityId
        DECLARE temp_tId DECIMAL(21,0) default -1;#occupyWild表的TroopsId  
            DECLARE temp_cid DECIMAL(21,0) default -1;#occupyWild表的CityId
            DECLARE tId2 DECIMAL(21,0) default -1;#troops表的TroopsId 
            DECLARE num0 int default 0;
            DECLARE num1 int default 0;
            DECLARE num2 int default 0;
            DECLARE num3 int default 0;
            DECLARE curl CURSOR FOR SELECT TroopsId,CityId FROM occupyWild order by TroopsId ASC;  
            DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET tId = -1;
            OPEN curl;  
        FETCH curl INTO tId,cid;  
        WHILE(tId<>-1) DO  
                #说明:游标变量使用一次以后就会被清除    
          #************************ 逻辑 START ************************
                    set tId2=-1,temp_tId=-1,temp_cid=-1,num0=0,num1=0,num2=0,num3=0;
                    set temp_tId=tId;
                    set temp_cid=cid;
                    select id into tId2 from Troops where isFrist=1 and CityId=temp_cid limit 1;#正常情况下只有一条数据
                    select number into num0 from Armforce where TroopsId=temp_tId and ArmType=0;
                    select number into num1 from Armforce where TroopsId=temp_tId and ArmType=1;
                    select number into num2 from Armforce where TroopsId=temp_tId and ArmType=2;
                    select number into num3 from Armforce where TroopsId=temp_tId and ArmType=3;
                    update Armforce set Number=Number+num0 where TroopsId=tId2 and ArmType=0;#退回军队
                    delete from Armforce where TroopsId=temp_tId and ArmType=0;#删除军队
                    update Armforce set Number=Number+num1 where TroopsId=tId2 and ArmType=1;
                    delete from Armforce where TroopsId=temp_tId and ArmType=1;
                    update Armforce set Number=Number+num2 where TroopsId=tId2 and ArmType=2;
                    delete from Armforce where TroopsId=temp_tId and ArmType=2;
                    update Armforce set Number=Number+num3 where TroopsId=tId2 and ArmType=3;
                    delete from Armforce where TroopsId=temp_tId and ArmType=3;
                    
                #************************ 逻辑 END ************************
    
          FETCH curl into tId,cid;  
        END WHILE;  
        CLOSE curl;  
    
    END
  • 相关阅读:
    Hadoop 组成
    umlの活动图
    一种基于ES5的JavaScript继承
    Unity3.x游戏开发经典教程 书例 100%完毕~
    AndroidMainifest标签使用说明1——&lt;action&gt;
    JS图片自己主动轮换效果实现
    现有一些开源ESB总线的比較
    安装程序不能验证Update.inf文件的完整性,请确定加密服务正在此计算机上执行
    leetCode解题报告5道题(九)
    浅谈C++多态性
  • 原文地址:https://www.cnblogs.com/zhuawang/p/4067181.html
Copyright © 2011-2022 走看看