mysql04--存储过程

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介:
复制代码
过程:若干语句,调用时执行封装的体。没有返回值的函数。
函数:是一个有返回值的过程

存储过程:把若干条sql封装起来,起个名字(过程),并存储在数据库中。

也有不存储的过程,匿名过程,用完就扔(mysql不支持匿名过程)


create procedure p1()
begin
    select 2+3; 
end$

show procedure status;//查看现有的存储过程:

mysql> call p1();//调用存储过程,Call 存储过程名字();

//存储过程中,使用declare声明变量,declare n int [default 默认值]

create procedure p2(age int,hei int)
begin
    declare age smallint default 3;
    declare height int default 180;
    select concat('年龄是:', age, '身高是:' ,height);
end$

mysql> call p2(1,2)$
+---------------------------------------------+
| concat('年龄是:', age, '身高是:' ,height) |
+---------------------------------------------+
| 年龄是:3身高是:180                        |
+---------------------------------------------+

create procedure p3()
begin
    declare age smallint default 3;
    set age := (age +20);
    select concat('年龄是:', age);
end$

mysql> call p3()$
+-------------------------+
| concat('年龄是:', age) |
+-------------------------+
| 年龄是:23              |
+-------------------------+

create procedure p4(n int)
begin
    select * from orde where gid=n;
end$

mysql> call p4(3)$
+-----+-----+-----+
| oid | gid | num |
+-----+-----+-----+
|   1 |   3 | 100 |
|   1 |   3 | 100 |
|   1 |   3 | 100 |
|   1 |   3 | 100 |
+-----+-----+-----+

create procedure p5(n int)
begin
    declare age int default 18;
    if age>18 then
        select '已成年';
    else
        select '未成年';
    end if;
end$



create procedure p7(n int,m char(1))
begin
if m='t' then
   select * from orde where gid=3;
else
   select * from orde where gid=2;
end if;
end$

delimiter $
create procedure p8(width int,height int)
begin
    if width > height then
        select '';
    elseif width < height then
        select '';
    else
        select ''
    end if;
end$

//编程:顺序、选择、循环。语法格式不一样。
create procedure t8()
begin
    declare total int default 0;
    declare num int default 0;
    
    while num <= 100 do
        set total := total + num;
        set num = num +1;
    end while;
    
    select total;
end$

mysql> call t8()$
+-------+
| total |
+-------+
|  5050 |
+-------+


create procedure t8(in n int)//in表示传进去的参数,
begin
    declare total int default 0;
    declare num int default 0;
    
    while num <= n do
        set total := total + num;
        set num = num +1;
    end while;
    
    select total;
end$

mysql>  create procedure t8(in n int,out total int)//in表示传进去的参数,out是传出去的参数,
begin
    declare num int default 0;
    
    set total=0;
    while num <= n do
        set num = num +1;
        set total := total + num;
    end while;

end$

mysql> call t8(100,@tt)$ //输出的值给@tt
Query OK, 0 rows affected

mysql> select @tt$
+------+
| @tt  |
+------+
| 5151 |
+------+


mysql>  create procedure t12(inout io1 int)//inout既可以传进去也可以传出来
    begin
    declare num int default 0;
    
    while num <= io1 do
        set num = num +1;
        set io1 := io1 + num;
    end while;

    end$

mysql> set @total = 100$
Query OK, 0 rows affected

mysql> call t12(@total)$
1264 - Out of range value for column 'io' at row 1

mysql> select @total$


//case用法:
create procedure t13()
begin
    declare pos int default 0;
    
    set pos := floor(4*rand());  //不能用position是关键字
    
    case pos
        when 1 then select "在飞";
        when 2 then select "在海里";
        when 3 then select "在地上";
        else select "不知道";
    end case;
    
end$

mysql> call t13()$
+--------+
| 不知道 |
+--------+
| 不知道 |
+--------+


//repeat
create procedure t14()
begin

    declare total int default 0;
    declare i int default 0;
    
    repeat
        set i:=i+1;
        set total:=total+i;
        until i>=100
    end repeat;

    select total;
end$

mysql> call t14()$
+-------+
| total |
+-------+
|  5151 |
+-------+
复制代码

 


本文转自农夫山泉别墅博客园博客,原文链接:http://www.cnblogs.com/yaowen/p/8144770.html,如需转载请自行联系原作者

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
16天前
|
存储 SQL 关系型数据库
轻松入门MySQL:加速进销存!利用MySQL存储过程轻松优化每日销售统计(15)
轻松入门MySQL:加速进销存!利用MySQL存储过程轻松优化每日销售统计(15)
|
1月前
|
存储 关系型数据库 MySQL
Mysql基础第二十六天,使用存储过程
Mysql基础第二十六天,使用存储过程
28 0
Mysql基础第二十六天,使用存储过程
|
1月前
|
存储 SQL 关系型数据库
【MySQL 数据库】9、存储过程
【MySQL 数据库】9、存储过程
204 0
|
3月前
|
存储 关系型数据库 MySQL
MySQL-调用存储过程
MySQL-调用存储过程
101 2
|
2月前
|
存储 SQL 关系型数据库
MySQL技能完整学习列表7、存储过程和函数——1、存储过程(Stored Procedures)的创建和执行——2、函数(Functions)的创建和使用
MySQL技能完整学习列表7、存储过程和函数——1、存储过程(Stored Procedures)的创建和执行——2、函数(Functions)的创建和使用
35 0
|
10天前
|
存储 SQL 关系型数据库
mysql存储过程示例
mysql存储过程示例
11 0
|
2月前
|
存储 SQL 关系型数据库
[MySQL]存储过程
[MySQL]存储过程
74 0
[MySQL]存储过程
|
2月前
|
存储 关系型数据库 MySQL
MySQL 中的存储过程-3
MySQL 中的存储过程
20 0
|
2月前
|
存储 SQL 关系型数据库
MySQL 中的存储过程-2
MySQL 中的存储过程
18 0
|
2月前
|
存储 关系型数据库 MySQL
MySQL 中的存储过程-1
MySQL 中的存储过程
39 2