利用存储过程修改字段类型

今天遇到需要把一个[color=blue]时间类型的字段改为整形[/color],但是又不能破坏原有的数据,即截取年份作为整形保存。设计了一段[color=red]存储过程[/color]完成了这个小小任务:
alter table p_artist drop column birthday ;
alter table p_artist Add column birthday int default 0 after kind;

[b]基本原理[/b]:先是备份了一份数据到另一个表中,然后从另一个表中取到对应的年份倒回来,保证数据没有被破坏;
[b]存储过程[/b]如下:
CREATE PROCEDURE updateartist ()
BEGIN
DECLARE birthday_temp int;
DECLARE temp_id bigint(20);
DECLARE find boolean default true;
DECLARE cur1 cursor for select YEAR(bt.birthday),bt.id from test.p_artist bt left join boss.p_artist tp on bt.id=tp.id;
DECLARE continue handler for not found set find=false;

open cur1;

while find = true do
fetch cur1 INTO birthday_temp,temp_id;
if birthday_temp is NULL then
set birthday_temp=0;
end if;
if find=true then
update boss.p_artist set birthday = birthday_temp where id=temp_id;
end if;
end while;
close cur1;
END;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值