☰
Mysql存储过程
2026/10/25 16:01:12 网站建设 项目流程

show global variables #查看全局变量
set global 全局变量 #设置全局变量的值
show session variables #查看会话变量

局部变量使用:

declare 定义局部变量

#创建存储过程say:delimiter // create procedure tarena.say48()begindeclarex int default9;declarey char(10);sety="plj";selectx,y;end // delimiter;#调用存储过程:call tarena.say48();

用户变量的使用(当前连接的用户有效):

定义:set @age = 19;
调用:select @age

参数的使用:

in: 把数据传给存储过程
out:接受存储过程处理的结果
inout:都有

IN:#根据部门编号统计部门的总人数delimiter // create procedure tarena.pnum(in dept_no int)beginselectdept_id as 部门编号,count(name)as 总人数 from employements wheredept_id=dept_no group by dept_id;end // delimiter;call tarena.pnum(6)
OUT:#根据员工姓名 获取员工的邮箱delimiter // create procedure tarena.pmail(in ename char(10), out yx char(50))beginselectemail into yx from employees wherename=ename;end // delimiter;call tarena.pmail("刘倩",@x);# @x保存处理的结果
INOUT: delimiter // create procedure tarena.myadd(inout i int)beginseti=i+100;end // delimiter;set@x=7;call tarena.myadd(@x);

存储过程-流程控制

if语句:

if条件1then语句1;elseif 条件2then语句2;else语句3;endif;

例子:

delimiter // create procedure tarena.deptype_pro(in no int,out dept_type varchar(5))begindeclaretypevarchar(5);selectdept_name intotypefrom departments wheredept_id=no;iftype='运维部'thensetdept_type='技术部';elseiftype='开发部'thensetdept_type='技术部';elseiftype='测试部'thensetdept_name='技术部';elsesetdept_name='非技术部';endif;end // delimiter;call tarena.deptype_pro(1,@t);

case语句:

case变量|表达式|字段 when 值1then返回值1;when 值2then返回值2;...else返回值n;endcase;

例子

delimiter // create procedure tarena.deptype_pro2(in no int,out dept_type varchar(5)) begin declare type varchar(5); select dept_name into type from departments where dept_id=no; case type when '运维部' then set dept_type='技术部'; when '开发部' then set dept_type='技术部'; when '测试部' then set dept_type='技术部'; else set dept_type='非技术部'; end case; end // delimiter ; call deptype_pro2(3,@t);

循环语句:

while循环

条件成立时,执行代码,条件不成立 会直接结束循环

while判断条件do代码 endwhile;

例子:

delimiter // create procedure tarena.while_pro(in i int)begindeclarej int default1;whilej<idoinsert into departments(dept_name)values('hr');setj=j+1;endwhile;end // delimiter;call while_pro(2);

loop循环

没有判断条件,重复执行同一段代码,只要不人为结束就一直执行,所以称为死循环。

loop 代码 end loop;

例子

delimiter // create procedure tarena.loop2()begindeclarei int default1;loopselectsleep(1), i;end loop;end // delimiter;call loop2;
mysql>show processlist;#相当于ps -auxmysql>killID号;

repeat循环

先循环一次,再判断条件,条件不成立继续执行,条件成立结束循环。

repeat 循环体until判断条件 end repeat;

例子:

delimiter // create procedure tarena.repeat_pro(in i int)begindeclarej int default1;repeatsetj=j+1;insert into departments(dept_name)values('sales');untilj>i;end repeat;end // delimiter;call repeat_pro(4);

循环控制语句:

leave

结束循环,相当于break

delimiter // create procedure tarena.p0()begin xixi:loop leave xixi;selectsleep(1);select"one";end loop xixi;end // delimiter;

iterate

终止当前循环并开始下次循环,相当于continue

delimiter // create procedure tarena.p3(in i int)begindeclarej int default1;haha:while j<=idoifj=3thensetj=j+1;iterate haha;endif;selectj;setj=j+1;endwhilehaha;end // delimiter;call p3(4);

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询