存储过程的作用:有助于提高应用程序的性能。存储过程可以不必发送多个冗长的SQL语句
废话不说多,直接实操
##实现num的相加
delimiter $$
CREATE PROCEDURE test1 ()
begindeclare num int default 0;		-- 声明变量,赋默认值为0select num+20;end $$
delimiter ;                 --将结束符修改成;call test1();			-- 调用存储过程
drop procedure test1   --如果不需要此存储函数开源删除咯set赋值操作
delimiter $$CREATE PROCEDURE test2 ()
begindeclare num int default 0;set num =50;			-- 给num变量赋值select num;end $$
delimiter ;call test2();
into的使用方法
delimiter $$
CREATE PROCEDURE test3 ()
begindeclare num int default 0;			select count(1) into num from t_student_info;  --计算t_student_info表的个数用num来记录select num;
end $$
delimiter ;
drop procedure test3;   删除该存储函数
call test3();
if的使用
delimiter $$
CREATE PROCEDURE test4 ()
begindeclare id int default 1;			declare class_name varchar(20);if id=1 thenset class_name='要多久你才可以爱上我!';elseif id=2 thenset class_name='不再让自己遗憾了';elseset class_name='不用想了,谁都不爱我,我只爱我自己';end if;select class_name;
end $$
delimiter ;call test4();mysql> delimiter;
 ERROR: 
 DELIMITER must be followed by a 'delimiter' character or string
使用delimiter;会报错,一定要带空格
  
 定义一个输入参数
delimiter $$
CREATE PROCEDURE test5 (in id int)
begindeclare class_name varchar(20);if id=1 thenset class_name='我和xhell脚本的if不一样!';elseif id=2 thenset class_name='我和python中的if语法有一点带你不一样';elseset class_name='不用想了,不靠别人';end if;select class_name;  
注:存储过程中声明了 class_name 变量并对其进行了赋值,但并没有通过 SELECT 语句来显示其值。你需要在存储过程末尾添加 SELECT class_name; 语句,以便在调用存储过程时返回 class_name 的值end $$
delimiter ;call test5(3);case的使用
delimiter $$
CREATE PROCEDURE test6 (in month int,out season varchar(10))
begincase when month >=1 and month<=3 thenset season='spring';when month >=4 and month<=6 thenset season='summer';when month >=7 and month<=9 thenset season='autumn';when month >=10 and month<=12 thenset season='winter';end case;
end $$
delimiter ;call test6(9,@season);			-- 定义会话变量来接收test8存储过程返回的值select @season;
@xxx:代表定义一个会话变量,整个会话都可以使用,当会话关闭(连接断开)时销毁
@@xxx:代表定义一个系统变量,永久生效。

while循环的使用
delimiter $$
CREATE PROCEDURE test7 (in count int)
begindeclare total int default 0;declare i int default 1;while i<=count doset total=total+i;set i=i+1;end while;select total;
end $$
delimiter ;call test7(10);

repeat的使用
delimiter $$
CREATE PROCEDURE test7 (count int)		  -- 默认是输入(in)参数
begindeclare total int default 0;repeat set total=total+count;set count=count-1;until count=0				-- 结束条件,注意不要打分号end repeat;select total;
end $$
delimiter ;call test8(10);使用 select total; 语句输出 total 的最终值。
total 是局部变量,只在该存储过程内部有效。
而 @total 是用户变量,可以在整个会话中使用和共享。
loop的使用
delimiter $$
CREATE PROCEDURE test9 (count int)		 -- 默认是输入(in)参数
begindeclare total int default 0;	sum:loop				-- 定义循环标识		 	set total=total+count;set count=count-1;if count < 1 thenleave sum;	 -- 跳出循环			end if;end loop sum;			 -- 标识循环结束		select total;end $$
delimiter ;call test9(10);
创建一张临时表:
create temporary table temp_table(id int,name varchar(10)
);
insert into temp_table values (1,'xiaoxiaowang');select * from temp_table ;
注意:临时表示查询不到的
show tables;   -- 不会显示临时表的存在
测试存储过程创建临时表是可以查到的,但是在存储函数中是查看不到的,结果会报错:
create procedure pro1()
begincreate temporary table temp_table(id int);insert into temp_table values(1);select * from temp_table;
end;call pro1();测试存储函数创建临时表
create function fun2()
returns int
begindeclare id int ;create table temp_table(				id int);insert into temp_table values(1);select id from into id temp_table;	return id;
end;
怎么查看自己创建了多少的存储过程????????????
SHOW PROCEDURE STATUS WHERE Db = 'your_database_name';
咱们的业务应该放到咱们的业务层,而不是把业务滞留到数据库来处理,将业务和数据库严重耦合在一起了!这是导致公司开发不使用存储过程的