开发者社区> 范大脚脚> 正文

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,如需转载请自行联系原作者

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
【MySQL】MySQL的存储过程(1)
【MySQL】MySQL的存储过程(1)
15 0
MySql轻功-存储过程
存储过程其实完全可以类比成高级语音里的方法和函数。这样看起来就不会抽象,也有个类比。通常函数体都会定义入参、出参,函数内部会定义该函数的计算过程,这里可能会有加减乘除、循环遍历、等等操作。
32 0
【MySQL】技多不压身,存储过程你会了吗?
今天我们来学习存储过程,在几年前的系统中存储过程随处可见,他的优势就是提高数据库的安全性和数据的完整性,我们可以利用存储过程完成一系列的流程而无需在代码中反复跳转,但是他的缺点也暴露了出来,开发与维护的难度时间越长难度越大,我们来看看他的神奇之处。
47 0
MySQL - 存储过程
MySQL - 存储过程
52 0
【MySQL】存储过程
【MySQL】存储过程
112 0
mysql的存储过程
mysql的存储过程
66 0
mysql 存储过程
mysql 存储过程 直接点,代码为主: 1.创建表 CREATE TABLE `t_tree` ( `id` int(11) NOT NULL AUTO_INCREMENT, `park_id` int(11) NOT NULL COMMENT '园区id', `parent_id.
1847 0
MySql 存储过程
自动增长列、字段值唯一性约束 create table aa( id int auto_increment primary key, sname varchar(32) unique ); insert into aa values(5,'abc');   创建一个自动增长的id属性(最开始不设置就从0开始增长) (后面的id如果有值了,如果添加数据时没有设置id,MySQL就会用最大的id加1做为最新的id) 注意:auto_increment 在MySQL中支持,其他的数据库设置自动增长列中关键字不一样。
922 0
+关注
范大脚脚
文章
问答
视频
文章排行榜
最热
最新
相关电子书
更多
从花农到MySQL大
立即下载
从华农到MySQL大神
立即下载
MySQL5.7 让优化更轻松
立即下载