首页 > 代码库 > mysql存储过程例子

mysql存储过程例子

/*
定义变量
方式1:set @变量名=值;
方式2:select 值 into @变量名;
方式3:declare 变量名 类型(字符串类型加范围) default 值;

in参数 入参的值会仅在存储过程中起作用
out参数 入参的值会被置为空,存储中计算的值会影响外面引用该变量的值
inout参数 入参的值不会被置为空,存储中计算的值会影响外面引用该变量的值
*/
use mysql;
/*创建1个存储过程*/
delimiter $$
DROP PROCEDURE IF EXISTS porc_person_02;
CREATE PROCEDURE porc_person_02(IN p1 INT, OUT p2 INT, INOUT p3 VARCHAR(20))
BEGIN
DECLARE innerp1 VARCHAR(10) DEFAULT ‘this is innerp1‘;
DECLARE innerp2 VARCHAR(10) DEFAULT ‘this is innerp2‘;
SET p1=10;
SET p2=20;
SET p3=‘this is 字符串‘;


if p1=10 then
select ‘p1 is 10‘;
end if;

if p1=p2 then
select ‘p1=p2‘;
else
select p1,p2,p3;
end if;

case p3
when ‘a‘ then
select ‘p3 is a‘;
when ‘b‘ then
select ‘p3 is b‘;
when ‘c‘ then
select ‘p3 is c‘;
else
select p3;
end case;

/*条件不满足会被终止*/
while p1>4
do
set p1=p1-1;
end while;
select p1;

checka:loop
set p1=p1+1;
if p1=14 then
leave checka;
end if;
end loop;
select p1;

/*条件满足会被终止*/
repeat
set p1=p1-1;
until p1=6
end repeat;
select p1;


END;
$$

set @p_in=3;
set @p_out=2;
set @p_inout=‘b‘;
select ‘check procedure‘ into @p4;

call porc_person_02(@p_in,@p_out,@p_inout);
select @p_in,@p_out,@p_inout,@p4;

mysql存储过程例子