服务器之家:专注于服务器技术及软件下载分享
分类导航

Mysql|Sql Server|Oracle|Redis|MongoDB|PostgreSQL|Sqlite|DB2|mariadb|Access|数据库技术|

服务器之家 - 数据库 - Mysql - MYSQL存储过程 注释详解

MYSQL存储过程 注释详解

2021-11-29 17:48拼命十三郎 Mysql

这篇文章主要介绍MYSQL存储的过程,本文围绕MYSQL存储详细展开文章内容,并附上详细代码解析,需要的下伙伴可以参考一下,希望对你有所帮助

0.环境说明:

 

软件 版本
mysql 8.0
navicat

 

1.使用说明

存储过程时数据库的一个重要的对象,可以封装sql语句集,可以用来完成一些较复杂的业务逻辑,并且可以入参出参(类似于java中的方法的书写)。

​ 创建时会预先编译后保存,用户后续的调用都不需要再次编译。

?
1
2
3
4
5
6
7
8
9
10
// 把edituser类比成一个存储过程
public void edituser(user user,string username){
    string a = "nihao";
    user.setusername(username);
}
main(){
    user user = new user();
 edituser(user,"张三");
    user.getuseranme();   //java基础
}

大家可能会思考,用sql处理业务逻辑还要重新学,我用java来处理逻辑(比如循环判断、循环查询等)不行吗?那么,为什么还要用存储过程处理业务逻辑呢?

优点:

在生产环境下,可以通过直接修改存储过程的方式修改业务逻辑(或bug),而不用重启服务器。
 执行速度快,存储过程经过编译之后会比单独一条一条执行要快。
 减少网络传输流量。
 方便优化。

缺点:

过程化编程,复杂业务处理的维护成本高。
 调试不便
 不同数据库之间可移植性差。-- 不同数据库语法不一致!

2.准备

数据库参阅资料中的sql脚本:

?
1
delimiter $$ --声明结束符

3.语法

官方参考网址:

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#### 3.0 语法结构
 
```sql
-- 存储过程结构
create
    [definer = user]
 procedure sp_name ([proc_parameter[,...]])
    [characteristic ...] routine_body
    
-- 1. proc_parameter参数部分,可以如下书写:
 [ in | out | inout ] param_name type
 -- type类型可以是mysql支持的所有类型
 
-- 2. routine_body(程序体)部分,可以书写合法的sql语句 begin ... end

简单演示:

?
1
2
3
4
5
6
7
8
9
-- 声明结束符。因为mysql默认使用‘;'作为结束符,而在存储过程中,会使用‘;'作为一段语句的结束,导致‘;'使用冲突
delimiter $$
 
create procedure hello_procedure ()
begin
 select 'hello procedure';
end $$
 
call hello_procedure();

3.1 变量及赋值

类比一下java中的局部变量和成员变量的声明和使用

局部变量:

用户自定义,在begin/end块中有效

语法:
声明变量 declare var_name type [default var_value];
举例:declare nickname varchar(32);

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
-- set赋值
delimiter $$
create procedure sp_var01()
begin
 declare nickname varchar(32) default 'unkown';
 set nickname = 'zs';
 -- set nickname := 'sf';
 select nickname;
end$$
-- into赋值
delimiter $$
create procedure sp_var_into()
begin
 declare emp_name varchar(32) default 'unkown' ;
 declare emp_no int default 0;
 select e.empno,e.ename into emp_no,emp_name from emp e where e.empno = 7839;
 select emp_no,emp_name;
end$$

用户变量:
用户自定义,当前会话(连接)有效。类比java的成员变量

语法:
@var_name
不需要提前声明,使用即声明

?
1
2
3
4
5
6
7
8
9
-- 赋值
delimiter $$
create procedure sp_var02()
begin
 set @nickname = 'zs';
 -- set nickname := 'sf';
end$$
call sp_var02() $$
select @nickname$$  --可以看到结果

会话变量:
由系统提供,当前会话(连接)有效

语法:

@@session.var_name

?
1
2
3
show session variables; -- 查看会话变量
select @@session.unique_checks; -- 查看某会话变量
set @@session.unique_checks = 0; --修改会话变量

全局变量:
由系统提供,整个mysql服务器有效

语法:
@@global.var_name

举例:

-- 查看全局变量中变量名有char的记录

?
1
2
3
4
show global variables like '%char%';
 
-- 查看全局变量character_set_client的值
select @@global.character_set_client;

3.2 入参出参

-- 语法
in | out | inout param_name type

举例:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
-- in类型演示
delimiter $$
create procedure sp_param01(in age int)
begin
 set @user_age = age;
end$$
call sp_param01(10) $$
select @user_age$$
-- out类型,只负责输出!
-- 需求:输出传入的地址字符串对应的部门编号。
delimiter $$
 
create procedure sp_param02(in loc varchar(64),out dept_no int(11))
begin
 select d.deptno into dept_no from dept d where d.loc = loc;
 --此处强调,要么表起别名,要么入参名不与字段名一致
end$$
delimiter ;
 
--测试
set @dept_no = 100;
call sp_param02('dallas',@dept_no);
select @dept_no;
-- inout类型
delimiter $$
create procedure sp_param03(inout name varchar(49))
begin
 set name = concat('hello' ,name);
end$$
delimiter ;
 
set @user_name = '小明';
call sp_param03(@user_name);
select @user_name;

3.3 流程控制-判断

官网说明
 

if

?
1
2
3
4
5
-- 语法
if search_condition then statement_list
    [elseif search_condition then statement_list] ...
    [else statement_list]
end if

举例:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
-- 前置知识点:timestampdiff(unit,exp1,exp2) 取差值exp2-exp1差值,单位是unit
select timestampdiff(year,e.hiredate,now()) from emp e where e.empno = '7499         ';
delimiter $$
-- drop procedure if exists sp_param04;
create procedure sp_param05(in ages timestamp)
begin
 declare result varchar(32);
 if timestampdiff(year,ages,now())>40
  then set result = '元老';
 elseif timestampdiff(year,ages,now())>38
  then set result = '老员工';
 else
  set result = '新手';
 end if;
 select result;
end $$
delimiter;
 
call sp_param05('1970-02-26 10:00:25');
-- 注意:mysql时间戳必须从1970年开始。

case

此语法是不仅可以用在存储过程,查询语句也可以用!

?
1
2
3
4
5
6
7
8
9
10
11
12
-- 语法一(类比java的switch):
case case_value
    when when_value then statement_list
    [when when_value then statement_list] ...
    [else statement_list]
end case
-- 语法二:
case
    when search_condition then statement_list
    [when search_condition then statement_list] ...
    [else statement_list]
end case

举例:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
-- 需求:入职年限年龄<=38是新手 >38并 <=40老员工 >40元老
delimiter $$
create procedure sp_hire_case()
begin
 declare result varchar(32);
 declare message varchar(64);
 case
    when timestampdiff(year,'2001-01-01',now()) > 40
  then
   set result = '元老';
   set message = '老爷爷';
 when timestampdiff(year,'2001-01-01',now()) > 38
  then
   set result = '老员工';
   set message = '油腻中年人';
 else
  set result = '新手';
  set message = '萌新';
 end case;
 select result;
end$$
delimiter ;

3.4 流程控制-循环

loop

?
1
2
3
4
-- 语法
[begin_label:] loop
    statement_list
end loop [end_label]

举例:

需要说明,loop是死循环,需要手动退出循环,我们可以使用leave来退出。

可以把leave看成我们java中的break;与之对应的,就有iterate(继续循环)——类比java的continue

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
--需求:循环打印1到10
-- leave控制循环的退出
delimiter $$
create procedure sp_flow_loop()
begin
 declare c_index int default 1;
 declare result_str  varchar(256) default '1';
 cnt:loop
 
  if c_index >= 10
  then leave cnt;
  end if;
 
  set c_index = c_index + 1;
  set result_str = concat(result_str,',',c_index);
  
 end loop cnt;
 
 select result_str;
end$$
 
-- iterate + leave控制循环
delimiter $$
create procedure sp_flow_loop02()
begin
 declare c_index int default 1;
 declare result_str  varchar(256) default '1';
 cnt:loop
 
  set c_index = c_index + 1;
  set result_str = concat(result_str,',',c_index);
  if c_index < 10 then
   iterate cnt;
  end if;
  -- 下面这句话能否执行到?什么时候执行到? 当c_index < 10为false时执行
  leave cnt;
  
 end loop cnt;
 select result_str;
 
end$$

repeat

MYSQL存储过程 注释详解

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[begin_label:] repeat
    statement_list
until search_condition -- 直到…为止,才退出循环
end repeat [end_label]
-- 需求:循环打印1到10
delimiter $$
create procedure sp_flow_repeat()
begin
 declare c_index int default 1;
 -- 收集结果字符串
 declare result_str varchar(256) default '1';
 count_lab:repeat
  set c_index = c_index + 1;
  set result_str = concat(result_str,',',c_index);
  until c_index >= 10
 end repeat count_lab;
 select result_str;
end$$

while

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
类比java的while(){}
[begin_label:] while search_condition do
    statement_list
end while [end_label]
-- 需求:循环打印1到10
delimiter $$
create procedure sp_flow_while()
begin
 declare c_index int default 1;
 -- 收集结果字符串
 declare result_str varchar(256) default '1';
 while c_index < 10 do
  set c_index = c_index + 1;
  set result_str = concat(result_str,',',c_index);
 end while;
 select result_str;
end$$

3.5 流程控制-退出、继续循环

leave

?
1
2
3
类比java的breake
-- 退出 leave can be used within begin ... end or loop constructs (loop, repeat, while).
leave label

iterate

?
1
2
3
类比java的continue
-- 继续循环 iterate can appear only within loop, repeat, and while statements
iterate label

3.6 游标

用游标得到某一个结果集,逐行处理数据。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
类比jdbc的resultset
-- 声明语法
declare cursor_name cursor for select_statement
-- 打开语法
open cursor_name
-- 取值语法
fetch cursor_name into var_name [, var_name] ...
-- 关闭语法
close cursor_name
-- 需求:按照部门名称查询员工,通过select查看员工的编号、姓名、薪资。(注意,此处仅仅演示游标用法)
-- 更改结束符为$$
delimiter $$
-- 创造存储过程(带一个入参)
create procedure sp_create_table02(in dept_name varchar(32))
begin
-- 必须先声明变量
 declare e_no int;
 declare e_name varchar(32);
 declare e_sal decimal(7,2);
 
 declare lp_flag boolean default true;
-- 其次声明游标:游标值为query(dept_name)得到的table(e.empno,e.ename,e.sal)
 declare emp_cursor cursor for
  select e.empno,e.ename,e.sal
  from emp e,dept d
  where e.deptno = d.deptno and d.dname = dept_name;
  
-- 然后声明 handler 句柄:
-- 关于句柄:https://blog.csdn.net/qq_43427482/article/details/109898934
-- 看完还没理解,再看:https://www.cnblogs.com/phpper/p/7587556.html
-- 这里涉及了sql state:https://blog.csdn.net/u014653854/article/details/78986780
-- 声明handler句柄:当每条sql传递error state为没有值的报错时,设定变量lp_flag为非真,同时继续执行sql(如不声明,当某条循环报错时,整个sql将直接停止循环)
 declare continue handler for not found set lp_flag = false;
-- 打开游标
 open emp_cursor;
-- 开启loop循环:emp_loop
 emp_loop:loop
-- 将游标值传递给三个变量
  fetch emp_cursor into e_no,e_name,e_sal;
-- 如果变量lp_flag为真,则获取这三个参数的值;否则打断emp_loop循环
  if lp_flag then
   select e_no,e_name,e_sal;
  else
   leave emp_loop;
  end if;
-- 结束循环
 end loop emp_loop;
-- 定义用户变量并赋值(用户变量不需要提前声明、仅当前会话有效)>鄙人没有理解这一步什么意义
 set @end_falg = 'exit_flag';
-- 关闭游标
 close emp_cursor;
-- 结束存储过程
end$$
-- 恢复;结束符
delimiter;
 
-- 使用该存储过程并传参
call sp_create_table02('research');

特别注意:

在语法中,变量声明、游标声明、handler声明是必须按照先后顺序书写的,否则创建存储过程出错。

3.7 存储过程中的handler

handler句柄用于定义条件处理

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
declare handler_action handler
    for condition_value [, condition_value] ...
    statement
 
handler_action: {
    continue  -- 继续执行
  | exit      -- 退出执行
  | undo      -- 什么都不做
}
 
continue: execution of the current program continues. -- 继续执行当前程序
exit: execution terminates for the begin ... end compound statement in which the handler is declared. this is true even if the condition occurs in an inner block. -- 停止执行在handler被声明的begin... end的组合程序,即使是在程序内部发生该条件。
 
condition_value: {
    mysql_error_code
  | sqlstate [value] sqlstate_value
  | condition_name
  | sqlwarning
  | not found
  | sqlexception
}
 
sqlwarning: shorthand for the class of sqlstate values that begin with '01'. -- 即sql state以01开头的集合代称
not found: shorthand for the class of sqlstate values that begin with '02'. -- 即sql state以o2开头的集合代称
sqlexception: shorthand for the class of sqlstate values that do not begin with '00', '01', or '02'. -- 即sql state不以00、01、02开头的集合代称
-- 各种写法:
 declare exit handler for sqlstate '42s01' set @res_table = 'exists';
 declare continue handler for 1050 set @res_table = 'exists';
 declare continue handler for not found set @res_table = 'exists';

4.练习

——大家注意,存储过程的业务过程在java代码中一般也可以实现,我们下面的需求是为了练习存储过程

4.1 利用存储过程更新数据

为某部门(需指定)的人员涨薪100;如果是公司总裁,则不涨薪。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
delimiter // -- 定义结束符
create procedure high_sal(in dept_name varchar(32)) -- 创建存储过程
begin -- 开始存储过程
 declare e_no int; -- 声明变量
 declare e_name varchar(32);
 declare e_sal decimal(7,2);
 
 declare lp_flag boolean default true;
 
 declare emp_cursor cursor for  -- 声明游标
  select e.empno,e.ename,e.sal
  from emp e,dept d
  where e.deptno = d.deptno and d.dname = dept_name;
  
 -- 声明handler句柄(条件处理)
 declare continue handler for not found set lp_flag = false;
  
 open emp_cursor; -- 打开游标
 
 emp_loop:loop -- 开启循环
  fetch emp_cursor into e_no,e_name,e_sal; -- 变量赋值
  
  if lp_flag then -- 流程控制
   if e_name = 'king' then
    iterate emp_loop; -- 继续循环
   else
    update emp e set e.sal = e.sal + 100 where e.empno = e_no; -- 更新数据
   end if;
  else
   leave emp_loop; -- 离开循环
  end if; -- 结束流程
  
 end loop emp_loop; -- 结束循环
 set @end_falg = 'exit_flag'; -- 声明用户变量
 close emp_cursor; -- 变比游标
end // -- 结束存储过程
delimiter; -- 恢复结束符
 
 
call high_sal('accounting');

4.2 循环创建表

创建下个月的每天对应的表comp_2020_04_01、comp_2020_04_02、...

(模拟)需求描述:

我们需要用某个表记录很多数据,比如记录某某用户的搜索、购买行为(注意,此处是假设用数据库保存),当每天记录较多时,如果把所有数据都记录到一张表中太庞大,需要分表,我们的要求是,每天一张表,存当天的统计数据,就要求提前生产这些表——每月月底创建下一个月每天的表!

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
-- 知识点 预处理 prepare语句from后使用局部变量会报错
-- https://dev.mysql.com/doc/refman/5.6/en/sql-prepared-statements.html
-- 看不懂英文文档的看这个:https://www.cnblogs.com/geaozhang/p/9891338.html;话说我也该去做一点英文技术文档的阅读提升了
prepare stmt_name from preparable_stmt
execute stmt_name [using @var_name [, @var_name] ...]
{deallocate | drop} prepare stmt_name
 
-- 贴一段预处理的案例,需求是:利用字符串定义预处理 sql (直角三角形斜边hypotenuse计算)
prepare stmt1 from 'select sqrt(pow(?,2) + pow(?,2)) as hypotenuse'; -- pow(x,y)函数,用于计算x的y次方(http://c.biancheng.net/mysql/pow_power.html);sqrt函数用于求平方根(https://blog.csdn.net/weixin_39554172/article/details/113124290)
set @a = 3;
set @b = 4;  -- 用户变量使用即声明
execute stmt1 using @a, @b; -- 结果为5
deallocate prepare stmt1;
 
-- 知识点 时间的处理
-- extract(unit from date)截取时间的指定位置值
-- date_add(date,interval expr unit)  日期运算
-- last_day(date)  获取日期的最后一天的日期
-- year(date) 返回日期中的年
-- month(date) 返回日期的月
-- dayofmonth(date) 返回日 -- 注:根据https://stackoverflow.com/questions/35838321/day-vs-dayofmonth-in-mysql,其实day(date)效果是一样的
-- 思路:循环构建表名 comp_2020_05_01 到 comp_2020_05_31;并执行create语句。
-- 分析:1. 循环构筑表,仅表名不同,考虑使用存储过程执行循环处理、使用预处理提高效率。 2. 首先需要一个变量存储执行sql;然后年份需要一个变量、月份需要一个变量、日期需要一个变量、拼接出来的表名需要一个变量;除此之外,还需要一个数字用来累增;至此,我们得到了至少需要6个变量。为了补全日期0,增加两个月份、日的变量用来补充0形成01、02···。 3. 考虑到预处的格式(from后不能有局部变量),写7个局部变量,1个用户变量
delimiter //  --声明结束符
create procedure sp_create_table()
begin
--  定义一堆局部变量
 declare next_year int;
 declare next_month int;
 declare next_month_maxday int;
  
 declare next_month_str char(2);
 declare next_month_maxday_str char(2);
 
 -- 处理每天的表名
 declare table_name_str char(10);
 -- 统计序列
 declare t_index int default 1;
 -- declare create_table_sql varchar(200);
 
 -- 获取下个月的年份
 set next_year = year(date_add(now(),interval 1 month));
 -- 获取下个月是几月
 set next_month = month(date_add(now(),interval 1 month));
 -- 下个月最后一天是几号
 set next_month_maxday = dayofmonth(last_day(date_add(now(),interval 1 month)));
 
 -- 把1-9月补上0:  01,02····,09
 if next_month < 10
  then set next_month_str = concat('0',next_month);
 else
  set next_month_str = concat('',next_month);
 end if;
 
 
 while t_index <= next_month_maxday do
  
  -- 同上,对天数补0
  if (t_index < 10)
   then set next_month_maxday_str = concat('0',t_index);
  else
   set next_month_maxday_str = concat('',t_index);
  end if;
  
  -- 2020_05_01
  set table_name_str = concat(next_year,'_',next_month_str,'_',next_month_maxday_str);
  -- 拼接create sql语句(用户变量)
  set @create_table_sql = concat(
     'create table comp_',
     table_name_str,
     '(`grade` int(11) null,`losal` int(11) null,`hisal` int(11) null) collate=\'utf8_general_ci\' engine=innodb');
  -- from后面不能使用局部变量!这就是我们为什么使用用户变量的原因
  prepare create_table_stmt from @create_table_sql;
  execute create_table_stmt;
  deallocate prepare create_table_stmt;
  
  set t_index = t_index + 1;
  
 end while;
end//
delimiter;
 
call sp_create_table()
 
-- 以下为个人精简版
 
delimiter //
create procedure sp_createtable1 () begin-- 统计序列
 declare
  t_index int default 1;
 while
   t_index <= day (
    last_day(
    date_add( now(), interval 1 month ))) do
   
   set @create_table_sql = concat(
    'create table comp_',
    year (
    date_add( now(), interval 1 month )),
    '_',
    month (
    date_add( now(), interval 1 month )),
    '_',
    t_index,
    '(`grade` int(11) null,`losal` int(11) null,`hisal` int(11) null) collate=\'utf8_general_ci\' engine=innodb'
   );-- from后面不能使用局部变量!这就是我们为什么使用用户变量的原因
  prepare create_table_stmt
  from
   @create_table_sql;
  execute create_table_stmt;
  deallocate prepare create_table_stmt;
  
  set t_index = t_index + 1;
  
 end while;
 
end //
delimiter;
call sp_createtable1 ()

4.3 其他场景:

“为用户添加购物积分,并更新到用户的总积分表中”等需要对多张表进行crud操作的业务。
而且内部可以使用事务命令。

5.其他

5.1 characteristic

?
1
2
3
4
5
6
characteristic:
    comment 'string'
  | language sql
  | [not] deterministic
  | { contains sql | no sql | reads sql data | modifies sql data }
  | sql security { definer | invoker }

其中,sql security的含义如下:

 mysql存储过程是通过指定sql security子句指定执行存储过程的实际用户;
 如果sql security子句指定为definer(定义者),存储过程将使用存储过程的definer执行存储过程,验证调用存储过程的用户是否具有存储过程的execute权限和definer用户是否具有存储过程引用的相关对象(存储过程中的表等对象)的权限;
 如果sql security子句指定为invoker(调用者),那么mysql将使用当前调用存储过程的用户执行此过程,并验证用户是否具有存储过程的execute权限和存储过程引用的相关对象的权限;
 如果不显示的指定sql security子句,mysql默认将以definer执行存储过程。

5.2 死循环处理

?
1
2
3
-- 如有死循环处理,可以通过下面的命令查看并结束
show processlist;
kill id;

5.3 可以在select语句中写case

?
1
2
3
4
5
6
7
8
9
https://dev.mysql.com/doc/refman/5.6/en/control-flow-functions.html
select
 case
  when timestampdiff(year,e.hiredate,now()) <= 38 then '新手'
  when timestampdiff(year,e.hiredate,now()) <= 40 then '老员工'
  else '元老'
 end hir_loc,
 e.*
from emp e;

5.4 临时表

临时表在关闭会话后会被自动销毁。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
create temporary table 表名(
  字段名 类型 [约束],
  name varchar(20)
)engine=innodb default charset utf8;
 
-- 需求:按照部门名称查询员工,通过select查看员工的编号、姓名、薪资。(注意,此处仅仅演示游标用法)
delimiter $$
create procedure sp_create_table02(in dept_name varchar(32))
begin
 declare emp_no int;
 declare emp_name varchar(32);
 declare emp_sal decimal(7,2);
 declare exit_flag int default 0;
 
 declare emp_cursor cursor for
  select e.empno,e.ename,e.sal
  from emp e inner join dept d on e.deptno = d.deptno where d.dname = dept_name;
 
 declare continue handler for not found set exit_flag = 1;
 
 -- 创建临时表收集数据
 create temporary table `temp_table_emp` (
  `empno` int(11) not null comment '员工编号',
  `ename` varchar(32) null comment '员工姓名' collate 'utf8_general_ci',
  `sal` decimal(7,2) not null default '0.00' comment '薪资',
  primary key (`empno`) using btree
 )
 collate='utf8_general_ci'
 engine=innodb;
 
 open emp_cursor;
 
 c_loop:loop
  fetch emp_cursor into emp_no,emp_name,emp_sal;
  
  
  if exit_flag != 1 then
   insert into temp_table_emp values(emp_no,emp_name,emp_sal);
  else
   leave c_loop;
  end if;
  
 end loop c_loop;
 
 select * from temp_table_emp;
 
 select @sex_res; -- 仅仅是看一下会不会执行到
 close emp_cursor;
 
end$$
 
call sp_create_table02('research');

附:本例建表sql

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
create table `dept` (
 `deptno` int(11) not null comment '部门编号',
 `dname` varchar(32) null comment '部门名称' collate 'utf8_general_ci',
 `loc` varchar(64) null comment '部门地址' collate 'utf8_general_ci',
 primary key (`deptno`) using btree
)
collate='utf8_general_ci'
engine=innodb
;
insert into dept values
 (10,'accounting','new york');
insert into dept values (20,'research','dallas');
insert into dept values
 (30,'sales','chicago');
insert into dept values
 (40,'operations','boston');
 
create table `emp` (
 `empno` int(11) not null comment '员工编号',
 `ename` varchar(32) null comment '员工姓名' collate 'utf8_general_ci',
 `job` varchar(10) null comment '职位' collate 'utf8_general_ci',
 `mgr` int(11) null comment '上级编号',
 `hiredate` date not null comment '入职时间',
 `sal` decimal(7,2) not null default '0.00' comment '薪资',
 `comm` decimal(7,2) null comment '年终奖金',
 `deptno` int(11) not null comment '部门编号',
 primary key (`empno`) using btree,
 index `fk_emp_dept` (`deptno`) using btree,
 constraint `fk_emp_dept` foreign key (`deptno`) references `procedure_demo`.`dept` (`deptno`) on update restrict on delete restrict
)
collate='utf8_general_ci'
engine=innodb
;
 
insert into emp values
(7369,'smith','clerk',7902,'1980-12-17',800,null,20);
insert into emp values
(7499,'allen','salesman',7698,'1981-02-20',1600,300,30);
insert into emp values
(7521,'ward','salesman',7698,'1981-02-22',1250,500,30);
insert into emp values
(7566,'jones','manager',7839,'1981-02-04',2975,null,20);
insert into emp values
(7654,'martin','salesman',7698,'1981-09-28',1250,1400,30);
insert into emp values
(7698,'blake','manager',7839,'1981-05-01',2850,null,30);
insert into emp values
(7782,'clark','manager',7839,'1981-06-09',2450,null,10);
insert into emp values
(7788,'scott','analyst',7566,'1987-07-13')-85,3000,null,20);
insert into emp values
(7839,'king','president',null,'1981-11-17',5000,null,10);
insert into emp values
(7844,'turner','salesman',7698,'1981-09-08',1500,0,30);
insert into emp values
(7876,'adams','clerk',7788,'1987-07-13')-51,1100,null,20);
insert into emp values
(7900,'james','clerk',7698,'1981-12-03',950,null,30);
insert into emp values
(7902,'ford','analyst',7566,'1981-12-03',3000,null,20);
insert into emp values
(7934,'miller','clerk',7782,'1982-01-23',1300,null,10);
 
create table `salgrade` (
 `grade` int(11) null,
 `losal` int(11) null,
 `hisal` int(11) null
)
collate='utf8_general_ci'
engine=innodb
;
insert into salgrade values (1,700,1200);
insert into salgrade values (2,1201,1400);
insert into salgrade values (3,1401,2000);
insert into salgrade values (4,2001,3000);
insert into salgrade values (5,3001,9999);

到此这篇关于mysql存储过程 注释详解的文章就介绍到这了,更多相关mysql存储过程内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://www.cnblogs.com/shixiu/p/15307012.html

延伸 · 阅读

精彩推荐