记一次使用mysql存储过程时,游标取值为空问题

call modify_collation(@num,@count_num)
> 1146 - Table 'test.table_name' doesn't exist
> 时间: 0.009s

 我在使用mysql存储过程时,打印时游标取值为空,报错找不到表。我的过程语句是这样的:

drop procedure if exists modify_collation;

DELIMITER //
create PROCEDURE modify_collation(out origin_num int(10),out count_num int(10))
COMMENT 'modify_collation' 
SQL SECURITY DEFINER
BEGIN
	DECLARE table_name VARCHAR(100);
	declare column_name VARCHAR(100);
	declare done int(10) default 0;
	declare collation_cursor cursor for SELECT table_name, column_name FROM INFORMATION_SCHEMA.COLUMNS 
	WHERE TABLE_SCHEMA = 'test' and collation_name = 'utf8mb4_0900_ai_ci';
	
	declare continue handler for not found set done = 1;
	
	select count(1) into origin_num from INFORMATION_SCHEMA.COLUMNS
	WHERE TABLE_SCHEMA = 'test' and collation_name = 'utf8mb4_0900_ai_ci';
	
	select origin_num;
	
	open collation_cursor ;
	set count_num := 0;
	mylp:loop
		FETCH collation_cursor into table_name,column_name;
		
		select table_name,column_name;
 		ALTER TABLE table_name MODIFY column_name VARCHAR(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
		if done = 1 then
		 leave mylp;
		end if;
		set count_num := count_num +1;
	end loop;

	close collation_cursor;
	select count_num;
end//
DELIMITER ;

 执行了几遍,进入了loop循环,总是打印游标取值为空:

我执行定义游标时后面的 select 语句也能查询到数据,

SELECT table_name, column_name FROM INFORMATION_SCHEMA.COLUMNS 
	WHERE TABLE_SCHEMA = 'test' and collation_name = 'utf8mb4_0900_ai_ci';

最后发现,我定义的变量名字和查询的字段名字一样,所以单独查询sql的时候有数据,执行存储过程时,select后面查询的是变量,而不是表中的字段,变量没有赋值就查询的是空。

把定义的变量名字修改后,就正常了。

drop procedure if exists modify_collation;

DELIMITER //
create PROCEDURE modify_collation(out origin_num int(10),out count_num int(10))
COMMENT 'modify_collation' 
SQL SECURITY DEFINER
BEGIN
	DECLARE t_name VARCHAR(100);
	declare c_name VARCHAR(100);
	declare done int(10) default 0;
	declare collation_cursor cursor for SELECT table_name, column_name FROM INFORMATION_SCHEMA.COLUMNS 
	WHERE TABLE_SCHEMA = 'test' and collation_name = 'utf8mb4_0900_ai_ci';
	
	declare continue handler for not found set done = 1;
	
	select count(1) into origin_num from INFORMATION_SCHEMA.COLUMNS
	WHERE TABLE_SCHEMA = 'test' and collation_name = 'utf8mb4_0900_ai_ci';
	
	select origin_num;
	
	open collation_cursor ;
	set count_num := 0;
	mylp:loop
		FETCH collation_cursor into t_name,c_name;
		
		select t_name,c_name;
-- 		ALTER TABLE t_name MODIFY c_name VARCHAR(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
		if done = 1 then
		 leave mylp;
		end if;
		set count_num := count_num +1;
	end loop;

	close collation_cursor;
	select count_num;
end//
DELIMITER ;

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DN金猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值