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$

  • 相关阅读:
    UVA 1515 Pool construction 最大流跑最小割
    BZOJ 1060: [ZJOI2007]时态同步 树形DP
    Codeforces Round #282 (Div. 1)B. Obsessive String KMP+DP
    BZOJ 4027: [HEOI2015]兔子与樱花 贪心
    BZOJ 2435: [Noi2011]道路修建 dfs搜图
    HDU 5297 Y sequence 容斥/迭代
    HDU 5296 Annoying problem dfs序 lca set
    HDU 5289 Assignment RMQ
    343D/Codeforces Round #200 (Div. 1) D. Water Tree dfs序+数据结构
    php mysqli扩展库之预处理操作
  • 原文地址:https://www.cnblogs.com/kingwwz/p/8493397.html
Copyright © 2011-2022 走看看