mysql 存储过程

本文深入讲解MySQL存储过程的创建、调用、删除及各种高级特性,包括变量声明、条件判断、循环控制、参数传递等,为数据库开发人员提供实用指南。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

delimiter $ 修改定界符

//创建存储过程
create procedure p1()
begin
select * from salary;
end$
//调用存储过程
call p1()$
//查看已经有的存储过程
show procedure status$
//删除
drop procedure sp_name
//引入变量
create procedure p1()
begin 
declare age int default 20;
declare height int default 180;
select CONCAT('年龄是:',age,'身高是:',height);
end$
//运算
create procedure p2()
begin 
declare age int default 20;
set age:=age+20;
select CONCAT('20年后年龄是:',age);
end$
//if else 语句
create procedure p3()
begin 
declare age int default 20;
if age>=18 then
	select '已成年';
else 
	select '未成年';
end if;
end$
//传参数[in out inout]
create procedure p4(width int,height int)
begin 
select concat("面积:",width*height);
end$
//in 参数
create procedure p6(in n int)
begin 
declare total int default 0;
declare num int default 0;
while num<=n do
	set total:=total+num;
	set num:=num+1;
end while;
select total;
end$
//out 参数
create procedure p7(in n int,out total int)
begin 
declare num int default 0;
set total:=0;
while num<=n do
	set total:=total+num;
	set num:=num+1;
end while;
end$
//inout
create procedure p8(inout age int)
begin
set age:=age+20;
end$
//while 循环
create procedure p5()
begin 
declare total int default 0;
declare num int default 0;
while num<=100 do
	set total:=total+num;
	set num:=num+1;
end while;
select total;
end$
//case 
create procedure p9(in num int)
begin 
case num
	when 1 then select 'one';
	when 2 then select 'two';
	when 3 then select 'three';
	else select 'other';
end case;
end$
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值