各位用户为了找寻关于MySQL 声明变量及存储过程分析的资料费劲了很多周折。这里教程网为您整理了关于MySQL 声明变量及存储过程分析的相关资料,仅供查阅,以下为您介绍关于MySQL 声明变量及存储过程分析的详细内容
声明变量
设置全局变量
set @a='一个新变量';
在函数和储存过程中使用的变量declear
declear a int unsigned default 1;
这种变量需要设置变量类型 而且只存在在 begin..end 这段之内
select .. into.. 直接将表内内容赋值到指定变量当中
select name,bid into @a,@b from bank limit 1;
要注意一点就是变量名不能和字段名一致
存储过程
存储过程将一段通用的操作封装在一起 这样再不同平台都可以公用了
储存过程没有返回值,而且不能sql语句调用,只能是call调用,而且不返回结果集,执行就执行了
要注意的是在储存过程中进行sql语句要用到 ; 这个系统默认结束符 要重新设置成别的,不然在写过程的一半系统就错认程序为终止继而报错
改变结束命令符为$
delimiter$+回车 或者简写成 d $+回车
显示所有存储过程
show procedure status;
删除指定存储过程
drop procedure 过程名;
存储过程演示'
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15d $ 1
create
procedure
yanshi(
in
arg tinyint)
begin
declare
age tinyint
default
0;
set
age=arg;
if age<20
then
select
'小于20的数'
;
elseif age>20
then
select
'大于20的数'
;
end
if;
end
$
//调用过程
set
@num=12$
call yanshi(@num)$
call yanshi(21)$
判断输入到存储过程中的数字属于哪个阶段
在存储过程中传参分 in ,out , inout 三种
in 可以输出从外部传入的变量 不会改变传进变量本来的值
? 1 2 3 4 5 6 7 8 9create
procedure
a(
in
id
int
)
begin
select
id;
set
id = 100;
end
$
set
@id=1$
call a(@id)$ //输出1 即从外部传进来的@id 的值
select
$id$ //输出1 说明存储过程中没有改变传进的值
out 不能输出从外部传进的值 会改变传进变量本来的值
? 1 2 3 4 5 6 7 8 9create
procedure
b(
out
id
int
)
begin
select
id;
set
id = 100;
end
$
set
@id=1$
call b(@id)$ //输入
null
select
@id$ //输出100
inout 就是又能输出传入变量又能改变传入变量咯
下面是检验你电脑硬件性能的时候了
还记得当年的bank表吗? 就是他保留住 然后执行以下命令:
? 1 2 3 4 5 6 7 8 9 10 11create
procedure
addbank()
begin
declare
i
int
default
0;
set
i = 5000000;
while i > 0 do
insert
into
bank (
name
)
values
(i);
set
i = i - 1;
end
while;
end
$
call addbank()$
祝你好运
总结
以上就是本文关于MySQL 声明变量及存储过程分析的全部内容,希望对大家有所帮助。感兴趣的朋友可以参阅:几个比较重要的MySQL变量 MySQL prepare原理详解 等,有什么问题可以随时留言,小编会及时回复大家的。感谢朋友们对网站的支持!
原文链接:http://www.cnblogs.com/gaofeifiy/p/5052434.html