mysql存贮过程的应用

##### in ###########
delimiter $$
create procedure delete_haha
(in no int)
begin
delete from ceshi.test where no!=t;
end $$
delimiter ;
call delete_haha(226);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
delimiter $$
drop procedure if exists test$$
create procedure test
(in id int,
in name varchar (20))
begin
insert into test values (id,name);
end$$
delimiter ;
call test(1,'slave');
select * from test;
######### if语句 ###############
delimiter $$
drop procedure if exists difference$$
create procedure difference
(in p1 int,
in p2 int,
out p3 varchar(20))
begin
if p1>p2
then
set p3='p1 big then p2';
elseif p1<p2
then
set p3='p1 small then p2';
else
set p3='cuo le ';
end if;
end$$
delimiter ;
call difference(1,2,@3);
select @3;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
delimiter $$
drop procedure if exists test$$
create procedure test
(inout a int,inout b int,inout c int)
begin
set c=a+b;
if
c>100
then
set c=c-100;
end if;
set a=b;
set b=c;
end $$
delimiter ;
set @a=16;
set @b=17;
call test (@a,@b,@s);
select @s;
call test (@a,@b,@s);
select @s;
call test (@a,@b,@s);
select @s;
call test (@a,@b,@s);
select @s;
############# declare 局部变量 #######################
delimiter $$
drop procedure if exists test$$
create procedure test
(out a int)
begin
declare b int
default (select count(*) from ceshi.test);
set a=b;
end $$
delimiter ;
call test (@s);
select @s;
########## case 语句 ####################
delimiter $$
drop procedure if exists difference$$
create procedure difference
(in p1 int,
in p2 int,
out p3 varchar(20))
begin
case
when p1&gt;p2 then set p3='p1 big then p2';
when p1<p2 then set p3='p1 small then p2';
else set p3='cuo le ';
end case;
end$$
delimiter ;
call difference(1,2,@3);
select @3;
########### while语句 #################
delimiter $$
drop procedure if exists difference$$
create procedure difference
(in p1 int,
in p2 int,
out p3 varchar(20))
begin
block1:begin
while
p1>p2
do
set p3='p1 big then p2';
leave block1;
end while;
end;
block2:begin
while p1<p2
do
set p3='p1 small then p2';
leave block2;
end while;
end;
end$$
delimiter ;
call difference(1,2,@3);
select @3;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
delimiter $$
drop procedure if exists test$$
create procedure test
(inout a int,inout b int,inout c int)
begin
test:while
c&lt;=100
do
set c=a+b;
set a=b;
set b=c;
end while;
if c>100
then
set c=c-100;
end if;
end $$
delimiter ;
set @a=16;
set @b=27;
call test (@a,@b,@s);
select @s;
call test (@a,@b,@s);
select @s;
call test (@a,@b,@s);
select @s;
call test (@a,@b,@s);
select @s;
############ repeat until 语句 ####################
delimiter $$
drop procedure if exists difference$$
create procedure difference
(in p1 int,
in p2 int,
out p3 varchar(20))
begin
block1:begin
repeat
set p3='p1 big then p2';
leave block1;
until p1&gt;p2
end repeat;
end;
block2:begin
repeat
set p3='p1 small then p2';
leave block2;
until p1<p2
end repeat;
end;
end$$
delimiter ;
call difference(1,2,@3);
select @3;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
delimiter $$
drop procedure if exists test$$
create procedure test
(inout a int,inout b int,inout c int)
begin
while c&lt;=100
do
set c=a+b;
set a=b;
set b=c;
if c>100
then
set c=c-100;
end if;
end while;
end $$
delimiter ;
set @a=16;
set @b=27;
call test (@a,@b,@s);
select @s;
############# loop语句 ################
delimiter $$
drop procedure if exists difference$$
create procedure difference
(in p1 int,
in p2 int,
out p3 varchar(20))
begin
block1:begin
loop
if p1&gt;p2
then
set p3='p1 big then p2';
end if;
leave block1;
end loop;
end;
block2:begin
loop
if p1<p2
then
set p3='p1 small then p2';
end if;
leave block2;
end loop;
end;
end$$
delimiter ;
call difference(1,2,@3);
select @3;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
delimiter $$
drop procedure if exists test$$
create procedure test
(inout a int,inout b int,inout c int)
begin
test:loop
if c>100
then
set c=c-100;
leave test;
end if;
set c=a+b;
set a=b;
set b=c;
end loop test;
end $$
delimiter ;
set @a=16;
set @b=27;
call test (@a,@b,@s);
select @s;
call test (@a,@b,@s);
select @s;
call test (@a,@b,@s);
select @s;
call test (@a,@b,@s);
select @s;
######### select into 语句 #####################
delimiter $$
drop procedure if exists test$$
create procedure test
(in a int,
out b int)
begin
select * into b from ceshi.test where no=a;
end $$
delimiter ;
call test (226,@a);
select @a;
####### 综合 #######################
delimiter $$
drop procedure if exists test$$
create procedure test
(inout c int)
begin
declare a,b int;
select no,id into a,b from haha;
if c&gt;1000
then
set c=c-1000;
end if;
set a=b,b=c;
update haha set no=a,id=b;
end $$
delimiter ;
call test(@a);
select @a;
############# declare handler #################
delimiter $$
drop procedure if exists test$$
create procedure test
(out error char(5))
begin
declare continue handler for sqlsate 'xxxxx'------------错误消息代码
set error='00000';
insert into haha values (229);
end $$
elimiter ;
call test(@a);
select @a;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
delimiter $$
drop procedure if exists test$$
create procedure test
(in id int,
in name varchar (20),
out error varchar(20))
begin
declare continue handler for sqlstate '23000'
set error='00000';
insert into test values (id,name);
end$$
delimiter ;
call test(1,'slave',@a);
select * from test;
select @a;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
delimiter $$
drop procedure if exists test$$
create procedure test
(in id int,
in name varchar (20),
out error varchar(20))
begin
declare continue handler for 1062 set error ='00000';
insert into test values (id,name);
end$$
delimiter ;
call test(1,'slave',@a);
select * from test;
select @a;
########## cursor游标 ---计算记录行数 ####################
delimiter $$
drop procedure if exists test$$
create procedure test
(out a int)
begin
declare c int;
declare found boolean default true;
declare b cursor for
select id from test;
declare continue handler for not found
set found =false;
set a=0;
open b;
fetch b into c;
while found do
set a=a+1;
fetch b into c;
end while;
close b;
end $$
delimiter ;
call test (@q);
select @q;

你可能感兴趣的:(数据库,mysql,应用,休闲,存贮)