编写了一个存储过程,逻辑是查询某一张表中部分字段的数据,然后插入到另外一张新表中。但是发现在操作过程,插入都是null。
1.存储过程
- #select * from tb_stu
-
-
- drop PROCEDURE IF EXISTS p_sync_stu;
- CREATE PROCEDURE p_sync_stu()
- begin
- -- 需要定义接收游标数据的变量
- declare id int(10);
- declare u_name varchar(255);
- declare u_age int(10);
- declare create_date TIMESTAMP;
- declare flag int default 0; -- 定义标记变量
- -- 创建游标,查询学生信息数据
- declare stu_data_List cursor for select u_name from tb_stu where create_time>='2022-07-27 10:00:00';
- -- 游标结束后,将标记量改为1
- declare exit handler for not found set flag=1;
- -- 开启游标
- open stu_data_List;
- -- 循环遍历游标
- repeat
- -- 使用游标,遍历结果数据
- fetch stu_data_List into u_name;
- -- 将数据保存到表中
- insert into tb_stu3 values(u_name);
- until flag=1
- end repeat;
- -- 关闭游标
- close stu_data_List;
- end
-
- call p_sync_stu();
2.截图
3.执行存储过程后,插入表中数据
经过千山万水的排查,终于发现定义游标的时候: declare stu_data_List cursor for select xx 的字段名和要声明的字段名称,不能一致,不能重名。
修改后的存储过程,再次执行,查看表中数据,如下图所示: 可以看到已经有了数据!