zoukankan      html  css  js  c++  java
  • [MySQL]存储过程

    存储过程语法:

    create procedure procedureName
    begin
    sql 1
    end$

    显示所有的存储过程   show procedure status;
    调用存储过程            call procedureName();

    包含if语句的存过
    create procedure procedureName()
    begin
    declare yy int
    if
    sql1
    else
    sql2
    end if;
    end$

    案例:

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

    编程三步骤
    顺序 选择 循环

    while循环语句的存过:
    求1到100的和;
    delimiter $
    create procedure procedurename ()
    begin
    declare total int default 0;
    declare num2 int default 0;
    while num2<=100  do
    set total:=total+num2;
    set num2:=num2+1;
    end while;
    select total;
    end$


     

    in/out/inout

    --in传入参数
    create procedure name (in num int)
    begin
    declare total int default 0;
    declare num2 int default 0;
    while num2<=num  do
    set total:=total+num2;
    set num2:=num2+1;
    end while;
    select total;
    end$


    --in/out
    delimiter$
    create procedure p8 (in num int,out total int )
    begin
    declare num2 int default 0;
    set total :=0;
    while num2<=num  do
    set total:=total+num2;
    set num2:=num2+1;
    end while;
    select total;
    end$
    调用方法:
    call p8(100,@tt)
    select @tt

    --inout
    delimiter$
    create procedure p9(inout age int)
    begin
    set age :=age+20;
    end$
    调用方法:
    set @currentage=18
    call p9(@currentage)
    select @currentage


    --case
    delimiter$
    create procedure p10()
    begin
    declare pos int default 0;
    set pos:=floor(4*rand());
    case pos
    when 1 then select 'happy';
    when 2 then select 'cry';
    when 3 then select 'angry';
    else select 'none';
    end case;
    end$


    --repeat循环
    delimiter$
    create procedure p11()
    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$

  • 相关阅读:
    ftp连接
    Excel表格内容导出到页面
    jquery 元素前或插入一元素
    sql 查出相同的记录 并把相同记录 显示在一起
    ie 使用window.open页面报错
    java生成word文档
    myeclipse导入项目
    java生成临时文件夹和删除临时文件夹
    正则学习笔记
    React和Vue的组件更新比较
  • 原文地址:https://www.cnblogs.com/kingwwz/p/8493397.html
Copyright © 2011-2022 走看看