mysql游标循环修改数据
Posted MyAzhe0ci3
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql游标循环修改数据相关的知识,希望对你有一定的参考价值。
CREATE PROCEDURE s
()
BEGIN
DECLARE user_ids BIGINT;
DECLARE mobile varchar(255);
DECLARE dones INT;
DECLARE sum INT DEFAULT(0);
DECLARE cur_user_id CURSOR FOR
SELECT user_id,SUBSTRING(payload->"
.
m
o
b
i
l
e
"
,
2
,
C
H
A
R
L
E
N
G
T
H
(
p
a
y
l
o
a
d
−
>
"
.mobile",2,CHAR_LENGTH(payload->"
.mobile",2,CHARLENGTH(payload−>".mobile")-2) AS mobile FROM hr_f_personal_archives_version WHERE payload->"
.
m
o
b
i
l
e
"
!
=
′
无
′
A
N
D
p
a
y
l
o
a
d
−
>
"
.mobile"!='无' AND payload->"
.mobile"!=′无′ANDpayload−>".mobile"!=‘None’ AND payload->"
.
i
d
e
n
t
i
t
y
"
I
N
(
S
E
L
E
C
T
p
a
y
l
o
a
d
−
>
"
.identity" IN(SELECT payload->"
.identity"IN(SELECTpayload−>".identity" AS ‘身份证号’ FROM hr_f_personal_basic_information hr WHERE payload->"
.
m
o
b
i
l
e
"
=
"
N
o
n
e
"
A
N
D
p
a
y
l
o
a
d
−
>
"
.mobile"="None" AND payload->"
.mobile"="None"ANDpayload−>".emergencyContactMobile"=“无”);
#游标中的内容执行完后将done设置为1
DECLARE CONTINUE HANDLER FOR NOT FOUND SET dones=1;
#打开游标
open cur_user_id;
#执行循环
posLoop:loop
set sum=sum+1;
#判断是否结束循环
if dones=1 then
leave posLoop;
END IF;
#取游标中的值
FETCH cur_user_id INTO user_ids,mobile;
update hr_f_personal_basic_information set payload=JSON_SET(payload, "$.mobile", mobile) where user_id=user_ids;
update hr_user_info set mobile= mobile where id=user_ids;
select user_ids,mobile;
#select user_ids,mobil;
END LOOP posLoop;
#释放游标
close cur_user_id;
select sum;
END
drop PROCEDURE s
call s
();
以上是关于mysql游标循环修改数据的主要内容,如果未能解决你的问题,请参考以下文章