时间:2021-07-01 10:21:17 帮助过:10人阅读
该方法只能在存储过程中(BEGIN...END内)定义局部变量,且会被默认初始化为NULL
2、SET(会话变量)
定义语句为:
- set @v_name = 1;
可以在任何地方声明,作为全局变量,需要进行初始化
二、变量赋值
MySQL中变量的赋值方法主要有两种:
1、select ... into [temp]
2、set [temp] = ...
三、输出变量
直接select想要输出的变量名
- set @result = null;
- call proc_calSumByInsWithOut(‘14365‘,@result);
- <span style="background-color: #ffff99">select @result;</span>
变量的值就会直接输出(注意call调用完存储过程后的分号;)
下面基于课本上university数据库,用临时变量(DECLARE)实现一个根据教师ID更改对应部门薪水总额的存储过程
- delimiter //
- create procedure proc_calSumSalaryByIns(in i_ID VARCHAR(5))
- begin
- <span style="background-color: #ffff99">declare d_name VARCHAR(20);
- </span> <span style="background-color: #ffff99"> select dept_name into d_name</span>
- from instructor
- where instructor.ID = i_ID;
- /*找出id为i_ID教师所在的部门*/
- update department as d set salary_sum =
- (select sum(instructor.salary)
- from instructor
- where instructor.dept_name = d_name)
- where d.dept_name = d_name;
- end
- //
- delimiter ;
MySQL - 变量
标签:res 全局 log roc als arch int sql 实现