当前位置:网站首页>MySQL stored procedure cursor traversal result set
MySQL stored procedure cursor traversal result set
2022-07-02 11:44:00 【Hundred million yards ahead】
Create stored procedure :
# Determine whether the stored procedure exists , There are deletions
DROP PROCEDURE IF EXISTS toCard;
CREATE DEFINER=`root`@`%` PROCEDURE `toCard`()
BEGIN
# Declaration end identifier
DECLARE end_flag int DEFAULT 0;
# Variable
DECLARE userId,cardThird bigint;
DECLARE cardHex,leftCard,rightCard,ecardNum VARCHAR(10);
# declare cursor user_curosr, take sql The result set is assigned to the cursor
DECLARE user_curosr CURSOR FOR SELECT id,ecardNumThird FROM u_user WHERE ecardNumThird IS NOT NULL AND ecardNumThird!='';
# Set termination ID
DECLARE CONTINUE HANDLER FOR NOT FOUND SET end_flag=1;
# Open cursor
OPEN user_curosr;
# Traversal cursor
REPEAT
# Get the current cursor pointer record , Get the value and assign it to the custom variable , notes : The variable name should not be the same as sql The returned column names are the same , The order of variables should be the same as sql The result columns are in the same order
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);
# Output results
#SELECT userId,cardThird,cardHex,leftCard,rightCard,ecardNum;
# Use the obtained value to operate the database
UPDATE u_user SET ecardNum=ecardNum WHERE id=userId;
# according to end_flag Judge whether it is over
UNTIL end_flag END REPEAT;
# Close cursor
close user_curosr;
ENDExecute stored procedures :
call toCard();It contains wiegand26 Card number conversion agreement
边栏推荐
猜你喜欢

From the perspective of attack surface, see the practice of zero trust scheme of Xinchuang

tidb-dm报警DM_sync_process_exists_with_error排查

tqdm的多行显示与单行显示

可昇級合約的原理-DelegateCall

Basic usage of MySQL in centos8

Importerror: impossible d'importer le nom « graph» de « graphviz»

念念不忘,必有回响 | 悬镜诚邀您参与OpenSCA用户有奖调研

RPA进阶(二)Uipath应用实践

Principle of scalable contract delegatecall
![[idea] use the plug-in to reverse generate code with one click](/img/b0/00375e61af764a77ea0150bf4f6d9d.png)
[idea] use the plug-in to reverse generate code with one click
随机推荐
Digital transformation takes the lead to resume production and work, and online and offline full integration rebuilds business logic
tqdm的多行显示与单行显示
mysql 基本语句
How to Create a Nice Box and Whisker Plot in R
What are the methods of adding elements to arrays in JS
ros缺少catkin_pkg
揭露数据不一致的利器 —— 实时核对系统
启牛商学院给的股票账户安全吗?能开户吗?
bedtools使用教程
亚马逊云科技 Community Builder 申请窗口开启
[idea] use the plug-in to reverse generate code with one click
PLC-Recorder快速监控多个PLC位的技巧
flutter 问题总结
Skills of PLC recorder in quickly monitoring multiple PLC bits
A white hole formed by antineutrons produced by particle accelerators
deepTools对ChIP-seq数据可视化
ImportError: cannot import name ‘Digraph‘ from ‘graphviz‘
ros gazebo相关包的安装
Is it safe to open a stock account online? I'm a novice, please guide me
The difference between SQL left join main table restrictions written after on and where