Mysql 使用遊標更新大量資料
阿新 • • 發佈:2020-09-09
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 SELECTid 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表是去取資料的表