mysql04--存储过程

简介:
复制代码
过程:若干语句,调用时执行封装的体。没有返回值的函数。
函数:是一个有返回值的过程

存储过程:把若干条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,如需转载请自行联系原作者

相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
MySQL数据库入门学习
本课程通过最流行的开源数据库MySQL带你了解数据库的世界。 &nbsp; 相关的阿里云产品:云数据库RDS MySQL 版 阿里云关系型数据库RDS(Relational Database Service)是一种稳定可靠、可弹性伸缩的在线数据库服务,提供容灾、备份、恢复、迁移等方面的全套解决方案,彻底解决数据库运维的烦恼。 了解产品详情:&nbsp;https://www.aliyun.com/product/rds/mysql&nbsp;
相关文章
|
10月前
|
存储 Java 关系型数据库
java调用mysql存储过程
在 Java 中调用 MySQL 存储过程主要借助 JDBC(Java Database Connectivity)。其核心原理是通过 JDBC 与 MySQL 建立连接,调用存储过程并处理结果。具体步骤包括:加载 JDBC 驱动、建立数据库连接、创建 CallableStatement 对象、设置存储过程参数并执行调用。此过程实现了 Java 程序与 MySQL 数据库的高效交互。
|
9月前
|
存储 关系型数据库 MySQL
【YashanDB知识库】MySQL返回结果集的存储过程的改写方法
本文介绍了将MySQL存储过程改写至YashanDB的解决方案。由于MySQL存储过程可直接返回结果集,而YashanDB需通过返回SYS_REF_CURSOR的函数实现类似功能,因此需要对代码进行转换。示例中展示了如何将MySQL存储过程`proc1`改写为YashanDB函数,并调整JDBC应用代码以适配REF_CURSOR输出参数,从而正确获取查询结果。此方法确保了跨数据库场景下的兼容性与功能性。
|
存储 SQL NoSQL
|
存储 SQL 关系型数据库
MySql数据库---存储过程
MySql数据库---存储过程
192 5
|
存储 关系型数据库 MySQL
MySQL 存储过程返回更新前记录
MySQL 存储过程返回更新前记录
236 3
|
存储 SQL 关系型数据库
MySQL 存储过程错误信息不打印在控制台
MySQL 存储过程错误信息不打印在控制台
279 1
|
存储 关系型数据库 MySQL
Mysql表结构同步存储过程(适用于模版表)
Mysql表结构同步存储过程(适用于模版表)
181 0
|
存储 SQL 关系型数据库
MySQL 创建存储过程注意项
MySQL 创建存储过程注意项
159 0
|
存储 SQL 关系型数据库
(十四)全解MySQL之各方位事无巨细的剖析存储过程与触发器!
前面的MySQL系列章节中,一直在反复讲述MySQL一些偏理论、底层的知识,很少有涉及到实用技巧的分享,而在本章中则会阐述MySQL一个特别实用的功能,即MySQL的存储过程和触发器。
372 0
|
存储 SQL 数据库
MySQL设计规约问题之为什么要避免使用存储过程、触发器和函数
MySQL设计规约问题之为什么要避免使用存储过程、触发器和函数

推荐镜像

更多