当前位置:网站首页>MySql存储过程游标遍历结果集
MySql存储过程游标遍历结果集
2022-07-02 09:38:00 【亿码当先】
创建存储过程:
#判断存储过程是否存在,存在删除
DROP PROCEDURE IF EXISTS toCard;
CREATE DEFINER=`root`@`%` PROCEDURE `toCard`()
BEGIN
#声明结束标识
DECLARE end_flag int DEFAULT 0;
#变量
DECLARE userId,cardThird bigint;
DECLARE cardHex,leftCard,rightCard,ecardNum VARCHAR(10);
#声明游标 user_curosr,将sql结果集赋值到游标中
DECLARE user_curosr CURSOR FOR SELECT id,ecardNumThird FROM u_user WHERE ecardNumThird IS NOT NULL AND ecardNumThird!='';
#设置终止标识
DECLARE CONTINUE HANDLER FOR NOT FOUND SET end_flag=1;
#打开游标
OPEN user_curosr;
#遍历游标
REPEAT
#获取当前游标指针记录,取出值赋给自定义的变量,注:变量名不要与sql返回的列名相同,变量顺序要和sql结果列的顺序一致
FETCH user_curosr INTO userId,cardThird;
SET cardHex=CONV(LEFT(cardThird, 8), 10, 16);
SET leftCard= CONV(LEFT(LPAD(cardHex,8,0) , 4), 16, 10);
SET rightCard= CONV(RIGHT(LPAD(cardHex,8,0) , 4), 16, 10);
SET ecardNum=LPAD(CONCAT(leftCard,rightCard),8,0);
#输出结果
#SELECT userId,cardThird,cardHex,leftCard,rightCard,ecardNum;
#利用取到的值进行数据库的操作
UPDATE u_user SET ecardNum=ecardNum WHERE id=userId;
# 根据 end_flag 判断是否结束
UNTIL end_flag END REPEAT;
#关闭游标
close user_curosr;
END
执行存储过程:
call toCard();
内包含wiegand26卡号转换协议
边栏推荐
猜你喜欢
PowerBI中导出数据方法汇总
Always report errors when connecting to MySQL database
ren域名有价值吗?值不值得投资?ren域名的应用范围有哪些?
制造业数字化转型和精益生产什么关系
V2x SIM dataset (Shanghai Jiaotong University & New York University)
[idea] use the plug-in to reverse generate code with one click
解决uniapp列表快速滑动页面数据空白问题
Mongodb learning and sorting (condition operator, $type operator, limit() method, skip() method and sort() method)
JS -- take a number randomly from the array every call, and it cannot be the same as the last time
由粒子加速器产生的反中子形成的白洞
随机推荐
揭露数据不一致的利器 —— 实时核对系统
Is bond fund safe? Does the bond buying foundation lose principal?
CTF record
II Stm32f407 chip GPIO programming, register operation, library function operation and bit segment operation
Pit of the start attribute of enumrate
TIPC Service and Topology Tracking4
微信小程序利用百度api达成植物识别
PHP tea sales and shopping online store
Why does LabVIEW lose precision in floating point numbers
What are the methods of adding elements to arrays in JS
TIPC introduction 1
PLC-Recorder快速监控多个PLC位的技巧
原生方法合并word
flutter 问题总结
enumrate的start属性的坑
Introduction to interface debugging tools
启牛商学院给的股票账户安全吗?能开户吗?
Always report errors when connecting to MySQL database
Implementation of six singleton modes
Skills of PLC recorder in quickly monitoring multiple PLC bits