• 企业400电话
  • 微网小程序
  • AI电话机器人
  • 电商代运营
  • 全 部 栏 目

    企业400电话 网络优化推广 AI电话机器人 呼叫中心 网站建设 商标✡知产 微网小程序 电商运营 彩铃•短信 增值拓展业务
    MySQL存储过程概念、原理与常见用法详解

    本文实例讲述了MySQL存储过程概念、原理与常见用法。分享给大家供大家参考,具体如下:

    1、存储过程的概念

    在一些语言中,如pascal,有一个概念叫“过程”procedure,和“函数”function,在php中,没有过程,只有函数。

    过程:封装了若干条语句,调用时,这些封装体执行
    函数:是一个有返回值的“过程”
    总结:过程是一个没有返回值的函数

    在MySQL中:

    我们把若干条sql封装起来,起个名字 —— 过程
    把此过程存储在数据库中 —— 存储过程

    2、创建存储过程

    create procedure procedureName()
    begin
      //--sql 语句
    end$
    
    

    3、查看已有的存储过程

    show procedure status
    
    

    4、删除存储过程

    drop procedure procedureName;
    
    

    5、调用存储过程

    call procedureName();
    
    

    6、第一个存储过程

    注意:我这里已经将MySQL的结束标识符改为$,如果要知道怎么设置为$,请参考我的另一篇文章:MySQL触发器。

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

    调用:

    call p1();
    
    

    显示结果:

    7、引入变量

    存储过程是可以编程的,意味着可以使用变量,表达式,控制结构来完成复杂的功能,在存储过程中,用declare声明变量:

    declare 变量名 变量类型 [default 默认值]

    使用:

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

    显示结果:

    8、引入表达式

    存储过程中,变量可以在sql语句中进行合法的运算,如+-*/。变量的赋值形式:

    set 变量名:= expression

    使用:

    create procedure p3()
    begin
      declare age int default 18;
      set age := age + 20;
      select concat('20年后年龄:',age);
    end$
    
    

    显示结果:

    9、引入选择控制结构

    格式:

    if condition then
      statement
    elseif
      statement
    else
      statement
    end if;
    
    

    使用:

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

    显示结果:

    10、给存储过程传参

    在定义存储过程的括号中,可以声明参数,语法:

    [in/out/inout] 参数名 参数类型

    使用:

    create procedure p5(width int,height int)
    begin
      select concat('你的面积是:',width * height) as area;
      if width > height then
        select '你比较胖';
      elseif width  height then
        select '你比较瘦';
      else
      select '你比较方';
      end if;
    end$
    
    

    显示结果:

    11、使用while循环结构

    需求:从1加到100

    使用:

    create procedure p6()
    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$
    
    

    显示结果:

    12、存储过程参数的输入输出类型

    主要有in、out、inout三种类型
    需求:从1加到N
    输入型的数据是我们给出值,输出型是我们给出变量名,用于乘装输出的变量值。

    (1)in型,此时in为输入行参数,它能接受到我们的输入

    create procedure p7(in n int)
    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$
    
    

    调用:

    call p7(100);
    
    

    输出结果:

    (2)out类型的参数

    create procedure p8(in n int,out total int)
    begin
      declare num int default 0;
      set total := 0;
      while num = n do
        set total := total + num;
        set num := num + 1;
      end while;
    end$
    
    

    调用:

    call p8(100,@total); --100为输入型参数,而@total为输出型变量
    select @total; --输出@total变量
    
    

    输出结果:

    (3)inout类型的参数

    create procedure p9(inout age int)
    begin
      set age := age+20;
    end$
    
    

    调用:

    set @age = 18; --设置@age变量为18
    call p9(@age); --调用p9存储过程,@age变量为实参
    select @age; --显示@age变量
    
    

    输出结果:

    inout型变量的实参也是一个变量名,这个变量在存储过程中既作为输入变量,又作为输出变量。

    13、case结构的用法

    使用:

    create procedure p10()
    begin
      declare pos int default 0;
      set pos := floor(5*rand());
      case pos
      when 1 then select '仍然在飞';
      when 2 then select '落在海里';
      when 3 then select '落在陆上';
      else select '我不知道在哪里';
      end case;
    end$
    
    

    输出结果:

    14、repeat循环结构

    格式:

    [begin_label:] REPEAT
      statement_list
    UNTIL search_condition
    END REPEAT [end_label]
    
    

    需求:从1加到100

    create procedure p11()
    begin
      declare total int default 0;
      declare num int default 0;
      r:repeat
        set total:= total + num;
      set num:=num + 1;
      until num > 100
      end repeat r;
      select total;
    end$
    
    

    输出结果:

    更多关于MySQL相关内容感兴趣的读者可查看本站专题:《MySQL存储过程技巧大全》、《MySQL常用函数大汇总》、《MySQL日志操作技巧大全》、《MySQL事务操作技巧汇总》及《MySQL数据库锁相关技巧汇总》

    希望本文所述对大家MySQL数据库计有所帮助。

    您可能感兴趣的文章:
    • mysql 存储过程中变量的定义与赋值操作
    • mysql存储过程 游标 循环使用介绍
    • MySQL存储过程使用实例详解
    • MySQL存储过程例子(包含事务,输出参数,嵌套调用)
    • MySql存储过程与函数详解
    • mysql 查询数据库中的存储过程与函数的语句
    • mysql 导入导出数据库以及函数、存储过程的介绍
    • MySQL 有输入输出参数的存储过程实例
    • MySQL 存储过程中执行动态SQL语句的方法
    • Mysql存储过程和函数区别介绍
    • MYSQL的存储过程和函数简单写法
    • MySQL存储过程中游标循环的跳出和继续操作示例
    上一篇:MySQL触发器概念、原理与用法详解
    下一篇:MySQL游标概念与用法详解
  • 相关文章
  • 

    © 2016-2020 巨人网络通讯 版权所有

    《增值电信业务经营许可证》 苏ICP备15040257号-8

    MySQL存储过程概念、原理与常见用法详解 MySQL,存储,过程,概念,原理,