mysql学习之写几个最简单的存储过程

写在前面:

文章整理上传于2017年9月26日 ,

是博主学习燕十八的视频后自己整理的笔记 ,如果有错误,你来打我呀

主要是存储过程的简单使用 ,至于什么是存储过程,为什么用过程,什么时候用过程,网上有好多,我不想在这里写了.


1.存储过程语法:

create procedure 过程名称(参数)
begin
//核心代码
end;
最简单的示例:

create procedure p1()
begin
select 'hello world';
end;
--调用: 
call p1;

2.声明变量

语法:
declare  变量1  类型  default  默认值 ;
例:
create procedure p1()
begin
declare  age  int  default  18;
select  age ;
end;

2.运算与变量赋值

赋值语法: set 变量名 := 运算 ;

create procedure p1()
begin
declare  age  int  default  18;
set  age := age + 20 ;
select  age ;
end;

3.逻辑控制 if-end if

--格式:
if  表达式  then 
--核心代码; 
end if ;

--例:
create procedure p1()
begin
declare  age  int  default  18;
set  age := age + 20 ;
if age>20 then
select '你>20了' ;
else if age<20 then
select '你<20' ;
else
select '你=20';
end if ;
end;

4.传参数,输入输出

--格式: 
[in/out/inout] 参数名称 类型 ,.....
--示例:
create procedure p1(in height int,in width int,out chengji)
begin
set chengji := height * width ;
if height>width then
select '你高>宽' ;
else if height<width then
select '高<宽' ;
else
select '高=宽';
end if ;
end;
--调用:
set @height :=10;
set @width :=20;
set @chengji :=0;
call p1(@height,@width,@chengji);
select @chengji;

5.流程控制 while

--格式: 
while 表达式 do 核心代码 end while;

--例:
create procedure p1(in i int)
begin
while i>0 do
select i;
set i := i-1;
end while;
end;

--调用:
call p1(10);

6多条件分支 case

语法:
 case 变量 when 值1 then  核心代码 ;
			when 值2 then  核心代码 ;
			else 核心代码 ;
end case ;

7.repeat循环

--语法: 
repeat 核心代码  until 表达式 end repeat ;

--例:
create procedure p1(in i int)
begin
while i>0 do
select i;
set i := i-1;
end while;
end;

8.0 游标 (sql数据处理 )

  --一条sql对应n个资源 ,取出资源的句柄/接口 就是游标
   --沿着游标 ,可以一行一行的取出数据
   
   --declare 声明游标,格式: declare 游标名称 cursor for select statement
   --open 打开,格式: open 游标名称
   --fetch 取值,格式: fetch 有标明 into var1,var2....
   --close 关闭,格式: close 游标名称
  --使用游标 ,主要是为了实现对数据的控制 ,控制数据进行改变数据,编程

8.1 存储过程中的游标(简单示例)

create procedure p1() 
begin
declare rowid int;
declare rownum int;
declare rowname varchar(20);
declare getgoods cursor for select *from good;--声明
open getgoods;
fetch getgoods into rowid,rownum,rowname;--取出赋值一次 ,可以循环多次直到游标到头(如何循环取出多行??)
close getgoods;--关掉
select rowid,rowname,rownum;--打印数据
end

8.2 游标+循环取出数据,越界标志

思路1:定义一个变量 count,使得count=总行数 ,然后while判断之类的直到fetch到count计数跳出
思路2:mysql的cursor中可以用declare continue handler 来操作一个越界标示
--declare continue handler for NOT FOUND set 越界变量 = 1 ;
--相当于发生越界404就把越界变量赋值为0;
--代码(sl2):
create procedure p1() 
begin
declare rowid int;
declare rownum int;
declare rowname varchar(20);
declare getgoods cursor for select *from good;--声明
declare yuejie int declare 0;--越界变量
declare continue handler for NOT FOUND set yuejie :=1;--越界变量
open getgoods;
repeat --循环开始
 fetch getgoods into rowid,rownum,rowname;--取出赋值
 select rowid,rowname,rownum;--打印数据()
until yuejie = 1 end repeat; --循环结束

close getgoods;--关掉
end
需完善的地方: 可能取出第一次就出现404 ,应当先fetch一次 
实际上,用while符合逻辑语义
当触发404 ,select仍会执行 ,select的数据为上次fetch的数据(缓存,暂且这样说)

8.2handler

continue: 触发后继续执行后续语句
exit: 触发后后续不在执行
undo: 触发后,前面的语句撤销(版本是否支持,有待考证)

9.后续补充

暂无.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章