Preface
This article mainly describes the method of mysql to query data and update to another table according to conditions
Implementation
CREATE DEFINER=`root`@`%` PROCEDURE `TaskTaxNumber`()
BEGIN
#Routine body goes here...
-- 需要定义接收游标数据的变量
DECLARE done BOOLEAN DEFAULT 0;
DECLARE c_tax_number VARCHAR(32); -- 纳税人识别号
DECLARE c_taxpayer_id VARCHAR(32);-- 纳税人Id
-- 定义游标
DECLARE cur1 CURSOR FOR SELECT tax_number c_tax_number,id c_taxpayer_id from t_taxpayer where tax_number is not null;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
-- 打开游标
OPEN cur1;
-- 开始循环
REPEAT
-- 获取游标变量值
FETCH cur1 INTO c_tax_number,c_taxpayer_id;
-- 判断是否读到游标末尾 如果不加这个条件 修改或插入的数据会增多执行1条
IF done <1 OR done >1 THEN
-- 修改任务表纳税人识别号字段的数据
UPDATE t_task set tax_number = c_tax_number where taxpayer_id = c_taxpayer_id;
END IF;
UNTIL done = 1
END REPEAT;
-- 循环结束
-- 关闭游标
CLOSE cur1 ;
END