MySQL清除所有注释的存储过程

该存储过程用于清除当前数据库中所有表的注释以及表内列的注释。它首先声明并打开两个游标,分别处理表的注释和列的注释。通过循环遍历游标,构造ALTERTABLESQL语句,然后执行这些语句以删除注释。存储过程使用了DECLARE、CURSOR、CONTINUEHANDLER等MySQL特性。
摘要由CSDN通过智能技术生成

CREATE DEFINER = ‘root’@‘%’
PROCEDURE test.sp_clearcomment()
COMMENT ‘清除所有注释’
BEGIN

DECLARE flag INT DEFAULT 0;

DECLARE tmp VARCHAR(1000) DEFAULT ‘’;

DECLARE cur CURSOR FOR SELECT concat(‘alter table ‘,table_schema,’.’,table_name,’ comment ‘’‘,’‘’;') s FROM information_schema. COLUMNS WHERE table_schema = DATABASE() GROUP BY TABLE_NAME;
DECLARE cur1 CURSOR FOR
select concat('alter table ', '',table_name,' ',
'modify column ', column_name,' ‘,
column_type,
ifnull(concat(’ character set ‘, character_set_name), ‘’),
ifnull(concat(’ collate ', collation_name), ‘’),
if(is_nullable = ‘no’, ’ not null ‘, ‘’),
if(column_default is null, ‘’, concat(’ default ‘, column_default)),
’ ‘, IF(extra=‘DEFAULT_GENERATED’,’’,extra), ‘;’)
as column_definition
from information_schema.columns
WHERE table_schema =DATABASE()
and column_comment is NOT NULL;

DECLARE CONTINUE HANDLER FOR NOT FOUND SET flag = 1;

OPEN cur;

WHILE flag != 1 DO

FETCH cur INTO tmp;

IF flag != 1 THEN

	set @V_P_SQL = tmp;   #需要先 赋值
	prepare stmt from @V_P_SQL;   #准备好sql语句
	EXECUTE stmt;      #执行sql

END IF;

END WHILE;

CLOSE cur;

set flag=0;

OPEN cur1;

WHILE flag != 1 DO

FETCH cur1 INTO tmp;

IF flag != 1 THEN

	set @V_P_SQL1 = tmp;  
	prepare stmt1 from @V_P_SQL1;      
	EXECUTE stmt1;      #执行sql

END IF;

END WHILE;

CLOSE cur1;

END

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值