mysql存储过程怎样变量赋值

 2022-07-25    553  

mysql存储过程怎样变量赋值

MySQL变量赋值就一个坑。这个坑有两种填法。

mysql存储过程怎样变量赋值

第一种是众所周知的:set 变量名=值/赋值语句

第二种是:select … into 变量名 …; 或者 select … into 变量名;

第一种,我在网上看很多人说变量名前面要加@符合,但是实际上是不必要的,只有一些特殊情况是必须要@符合,大部分时候不加也不影响存储过程的使用的。

例如:

set g_grant_ex='gamedb.tb_exchange_';
set @g_grant_ex='gamedb.tb_exchange_';
/*这其实是一样的效果*/

第二种,这个就更坑了。我之前看别人用的是云里雾里。后来动手试了几次才发现。。。。。。。蛮好用的~

例如:

1.select count(*) into g_err from information_schema.TABLES t where t.TABLE_SCHEMA='gamedb' and t.TABLE_NAME=concat('tb_recharge_',g_year);
2.select count(*) from information_schema.TABLES t where t.TABLE_SCHEMA='gamedb' and t.TABLE_NAME=concat('tb_recharge_',g_year) into g_err;
/*这两个效果也是一样的*/


  •  标签:  
  • mysql
  •  

原文链接:https://77isp.com/post/2247.html

=========================================

https://77isp.com/ 为 “云服务器技术网” 唯一官方服务平台,请勿相信其他任何渠道。