中局部变量定义在代码最前面,作用范围也是在此函数范围内。注意同全局变量和用户变量区分开。
使用declare关键字来声明变量
注意必须写在代码块的最前面
de count int DEFAULT 0;
DECLARE username varchar(32);
一次声明多个相同类型的变量
DECLARE p_parent int DEFAULT 0;
DECLARE parent0, parent1, parent2, parent3, parent4, parent5 int DEFAULT 0;
使用set来给变量赋值
set <变量名> = <值>
DECLARE sflag int default 0;
set sflag =100;
select 语句给变量赋值
select 列名 into 变量名,支持多个变量同时赋值。
SELECT col_name[, …] INTO var_name[,…] from table
SELECt IFNULL(cnt,0) into parent0 from temp_student where b = 0 limit 1;
select sname, sno into p_name, p_no from temp_student where id=888;
select 'result:', p_no, p_name;
游标赋值
DECLARE p_id int;
DECLARE p_name varchar(32);
DECLARE sflag int DEFAULT 0;
DECLARE cur_school CURSOR FOR select id,name from school where status=0;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET sflag = 1;
open cur_school;
fetch cur_school into p_id, p_name;
while sflag<>1 do
// ......
fetch cur_school into p_id, p_name;
select 'in while:',p_id, sflag;
end while;
close cur_school;