当前位置:Gxlcms > 数据库问题 > MySQL - 变量

MySQL - 变量

时间:2021-07-01 10:21:17 帮助过:10人阅读

v_name v_type;

该方法只能在存储过程中(BEGIN...END内)定义局部变量,且会被默认初始化为NULL

 

2、SET(会话变量)

定义语句为:

  1. set @v_name = 1;

可以在任何地方声明,作为全局变量,需要进行初始化

 

二、变量赋值

MySQL中变量的赋值方法主要有两种:

1、select ... into [temp]

2、set [temp] = ...

 

三、输出变量

直接select想要输出的变量名

  1. set @result = null;
  2. call proc_calSumByInsWithOut(‘14365‘,@result);
  3. <span style="background-color: #ffff99">select @result;</span>

变量的值就会直接输出(注意call调用完存储过程后的分号;)

 

下面基于课本上university数据库,用临时变量(DECLARE)实现一个根据教师ID更改对应部门薪水总额的存储过程

  1. delimiter //
  2. create procedure proc_calSumSalaryByIns(in i_ID VARCHAR(5))
  3. begin
  4. <span style="background-color: #ffff99">declare d_name VARCHAR(20);
  5. </span> <span style="background-color: #ffff99"> select dept_name into d_name</span>
  6. from instructor
  7. where instructor.ID = i_ID;
  8. /*找出id为i_ID教师所在的部门*/
  9. update department as d set salary_sum =
  10. (select sum(instructor.salary)
  11. from instructor
  12. where instructor.dept_name = d_name)
  13. where d.dept_name = d_name;
  14. end
  15. //
  16. delimiter ;

  

MySQL - 变量

标签:res   全局   log   roc   als   arch   int   sql   实现   

人气教程排行