Mysql 使用游標更新大量數據


 1 DROP PROCEDURE  if exists updateExecute;
 2 
 3 -- 創建存儲過程
 4 delimiter $
 5 create procedure updateExecute()
 6 begin
 7   declare cnt int DEFAULT 1000;
 8   declare i int default 0;
 9   DECLARE gfgl_code VARCHAR(50) DEFAULT '';
10   DECLARE F_01 VARCHAR(6) DEFAULT '';
11   DECLARE F_02 VARCHAR(6) DEFAULT '';
12   declare class_id int default 0;
13   
14   declare getgoods cursor for select `編碼`, F01, F02 from `gfgl`.`bm_zrmx_wz` LIMIT 0, 1000;
15   -- select count(*) into cnt from `gfgl`.`bm_zrmx_wz` LIMIT 1, 1000;
16   open getgoods;
17   repeat 
18     fetch getgoods INTO gfgl_code, F_01, F_02;
19     
20     SELECT id INTO class_id FROM OilGoodsAptitudeClass a WHERE a.Code=gfgl_code;
21     
22   UPDATE OilGoodsAptitude b SET
23    b.F01=(case F_01 when '' then '1' ELSE '' END),
24    b.F02='1',
25    b.F03='1',
26    b.F04=(case F_04 when '' then '1' ELSE '' END)
27    WHERE b.ClassId=class_id;
28 
29   set i:= i+1;
30   until i >= cnt end repeat;
31   close getgoods;
32 END$
call updateExecute()

參考:

https://www.jb51.net/article/164679.htm

b表是我要更新的表,a表和gfgl表是去取數據的表

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM