zoukankan      html  css  js  c++  java
  • mysql自定函数

    一、基本语法

      delimiter 自定义符号  -- 如果函数体只有一条语句, begin和end可以省略, 同时delimiter也可以省略

      create function 函数名(形参列表) returns 返回类型  -- 注意是retruns

      begin

        函数体    -- 函数内定义的变量如:set @x = 1; 变量x为全局变量,在函数外面也可以使用

        返回值

      end

      自定义符号

      delimiter ;

    二、示例

    -- 自定义函数
    delimiter $$
    create function myfun3(ia int, ib int) returns int
    begin
        return ia + ib;
    end
    $$
    delimiter ;

    三、查看函数
      1. show function status [like 'pattern'];  -- 查看所有自定义函数, 自定义函数只能在本数据库使用。

      2. show create function 函数名;  -- 查看函数创建语句

    四、删除函数

      drop function 函数名;

    五、综合应用

    1. 使用全局变量

    -- 计算1 ~ 指定数据之间的和
    delimiter $$
    create function my_sum(x int) returns int
    begin
        set @i = 1;
        set @sum = 0;
        while @i <= x do
            set @sum = @sum + @i;
            set @i = @i + 1;
        end while;
        return @sum;
    end
    $$
    delimiter ;

     2. 使用局部变量

    -- 求1 ~ 指定数之前的和,但5的倍数不加
    delimiter $$
    create function my_sum2(x int) returns int
    begin
        declare i int default 1;
        declare sum int default 0;
        sumwhile:while i <= x do
            if i % 5 = 0 then
                set i = i + 1;
                iterate sumwhile;
            end if;
            set sum = sum + i;
            set i = i + 1;
        end while;
        return sum;
    end
    $$
    delimiter ;

    转自与:https://www.cnblogs.com/pengyin/p/6395652.html
  • 相关阅读:
    css笔记
    CSS基础
    HTML常用基础标签
    React-Native中列表SectionList学习
    React-Native中列表FlatList配合网络请求fetch学习
    React-Native选项卡(react-navigation)之createBottomTabNavigator学习
    React-Native导航条(react-navigation)之createStackNavigator学习
    iOS中touch事件传递(响应者链)
    ios中UIView和CALayer关系
    iOS中KVO使用理解
  • 原文地址:https://www.cnblogs.com/hellohero55/p/12117235.html
Copyright © 2011-2022 走看看