mysql创建存储过程
Posted xuzhujack
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql创建存储过程相关的知识,希望对你有一定的参考价值。
今天开发一个需求,需要在一个旧表中增加一列并且对已经的表中记录初始化新列的值,
由于是一次性的工作,故写了个存储过程来代替代码程序初始化
创建及执行过程记录如下:
mysql [XXX_YYY]> delimiter $$
MySQL [XXX_YYY]>CREATE PROCEDURE `updateShopItemXXSkuIdV2`()
-> BEGIN
-> #Routine body goes here...
-> declare i int;
-> set i=0;
-> set @sum=(select count(1) from shop_item_info where xx_sku_id is null and yn = 1);
-> set @log = "log query ....";
-> select CONCAT(@log,@sum," 条");
-> while i<@sum do
-> set @itemId =( select item_id from shop_item_info where xx_sku_id is null and yn = 1 limit i,1);
-> set @jdSkuId =( select jd_sku_id from item_import_table where item_id = @itemId limit 1);
-> #set @itemId = 4499991;
-> #set @xxSkuId =29825634;
-> #select @itemId , @jdSkuId;
-> if @itemId is NULL THEN
-> select @itemId ;
-> elseif @xxSkuId is NULL THEN
-> select @xxSkuId;
-> else
-> update shop_item_info set xx_sku_id = @xxSkuId ,modified = now() where item_id = @itemId and yn = 1;
-> end if;
-> set i=i+1;
-> end while;
->
-> END$$
Query OK, 0 rows affected (0.01 sec)
MySQL [XXX_YYY]> call updateShopItemXXSkuIdV2()$$
+--------------------------+
| CONCAT(@log,@sum," 条") |
+--------------------------+
| log query ....126 条 |
+--------------------------+
MySQL [XXX_YYY]> delimiter ;
ps:创建存储过程可以用navicate等客户端工具生成调试,然后把调试好的存储过程脚本,在生产环境中重写一下就好。
以上是关于mysql创建存储过程的主要内容,如果未能解决你的问题,请参考以下文章