SQL存储过程例子
https://www.jianshu.com/p/77c888044efd
1.概念
一组为完成特定功能的SQL语句集,存储在数据库中,经过第一次编辑后再次调用不许再次编辑,用户通过指定存储过程的名称并给出参数(如果有参数)来执行它,它可包括逻辑控制语句和数据操作语句,它可接受参数、输出参数、返回单个或多个结果集以及返回值。
--创建
create procedure GetUsers()
begin
select * from users;
end;
--调用
call GetUsers();
--删除
drop procedure if exists GetUsers;
4.创建复杂示例
delimiter //
create procedure GetPriceByID(
in prodID int,
in isDisc boolean,
out prodPrice decimal(8,2))
begin
declare tempPrice decimal(8,2);#声明变量
declare prodDiscRate decimal(8,2);
set prodDiscRate = 0.88;
select price from products
where id = prodID
into tempPrice;
if isDisc then
select tempPrice * prodDiscRate into tmpPrice;
end if;
select tmpPrice into prodPrice;
end;
//
调用:
call GetPriceByID(1,ture,@prodPrice);
select @prodPrice;
技术细节:
MySQL 存储过程
https://www.runoob.com/w3cnote/mysql-stored-procedure.html