• mysql 存储过程和游标


    CREATE DEFINER=`root`@`localhost` PROCEDURE `NewProc`()
    BEGIN
        #Routine body goes here...
            DECLARE  itemId varchar(64);   -- id
            DECLARE  AA varchar(64);   -- 省
        DECLARE  BB  varchar(64); -- 市
        DECLARE  CC  varchar(64); -- 区
            DECLARE  new_province varchar(64);   -- 省
        DECLARE  new_city  varchar(64); -- 市
        DECLARE  new_area  varchar(64); ---- 遍历数据结束标志
        DECLARE done INT DEFAULT FALSE;
        -- 游标
        DECLARE cur_account CURSOR FOR select id,province,city,area from startup_project_copy;
        -- 将结束标志绑定到游标
        DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
        
        -- 打开游标
        OPEN  cur_account;     
        -- 遍历
        read_loop: LOOP
                -- 取值 取多个字段
            FETCH  NEXT from cur_account INTO itemId,AA,BB,CC;
    
                IF done THEN
                    LEAVE read_loop;
                END IF;
                    SET new_province = (SELECT Impcode FROM sys_area WHERE AreaID = AA);
                    SET new_city = (SELECT Impcode FROM sys_area WHERE AreaID = BB);
                    SET new_area = (SELECT Impcode FROM sys_area WHERE AreaID = CC);
    -- 你自己想做的操作
                    --                  INSERT INTO test_city VALUE(new_province, new_city, new_area, itemId);
                     -- 执行更新
                     UPDATE startup_project_copy SET province = new_province, city= CONCAT(new_city,'000000'), area = CONCAT(new_area,'000000') WHERE id = itemId;
    --                  SET @UPDATE = CONCAT('UPDATE startup_project_copy SET province =', new_province,', city=', new_city, ', area =', new_area, ' WHERE id = ',id);
    --                 PREPARE stm FROM @UPDATE;
    --                 EXECUTE stm;
    --                 DEALLOCATE PREPARE stm;
                     COMMIT;-- 提交
        END LOOP;
     
        CLOSE cur_account;
        
    
    END
  • 相关阅读:
    Boost.Asio c++ 网络编程翻译(10)
    建站手册:网站品质
    建站手册-template
    CDN:分类
    CDN:BootCDN 项目列表-摘录-20180405
    CDN:BootCDN
    CDN:目录
    CDN-template
    JavaScript-Tool:md5.js
    Regexp-Utils:基本
  • 原文地址:https://www.cnblogs.com/joyny/p/10153941.html
Copyright © 2020-2023  润新知