• 数据库函数


    #定义函数:

    delimiter //   #定义结束符
    create function myadd(a int , b int)
    returns int
    begin
    declare c int default 0;
    set c = a + b;
    return c;
    end
    //
    delimiter ;  #将结束符定义回来
    select myadd(3,4);

    #会话变量
    set @b = myadd(3,4);
    select @b;

    #if的使用

    delimiter //
    create function myfunc(a int )
    returns varchar(10)
    begin
    declare c varchar(10) default '';
    if(a>0) then set c = '大于0';
    elseif(a=0) then set c = '等于0';
    else set c = '等于0';
    end if;
    return c;
    end
    //
    delimiter ;
    select myfunc(3);

    #删除函数

    drop function if exists myfunc;

    #case

    1.    delimiter //
      create function myfun(a int)
      returns varchar(10)
      begin
        declare c varchar(10) default '';
        case when a >0 then set c = 'dayu0';
        when a =0 then set c = 'dengyu0';
        else set c = 'xiaoyu0';
        end case;
        return c;
      end //
      delimiter ;
    drop function if exists myfun;
    2. delimiter //
      create function myfun(a int)
      returns varchar(10)
      begin
        declare c varchar(10) default '';
        case a when 1 then set c = 'dayu0';
        when 2 then set c = 'dengyu0';
        else set c = 'xiaoyu0';
        end case;
        return c;
      end //
      delimiter ;

    #while do

    delimiter //
    create function myfun(a int)
    returns int
    begin
      declare c int default 0;
      declare i int default 1;
      out_label :BEGIN
      while 1=1 do
      set c= c+i;
      set i = i+1;
      if(i>=a) then leave out_label;
      end if;
      end while;

      END out_label;
      return c;
    end //
    delimiter ;

  • 相关阅读:
    面向对象编程思想(一)
    IT第十九天
    IT第十八天
    关于面试,来自无锡一位尊者的建议
    IT第十一天、第十二天、第十三天
    数据结构 3动态规划
    java 零碎1
    数据结构 2.迭代与递归
    数据结构 1.算法分析
    java 字符串(正则表达式)未完
  • 原文地址:https://www.cnblogs.com/Lune-Qiu/p/8666163.html
Copyright © 2020-2023  润新知