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