MySQL 创建自定义函数(1)

时间:2023-01-18 19:39:15

1. 创建测试自定义函数(1)

CREATE DEFINER=`dbdh`@`localhost` FUNCTION `test`.`sp_function_dbdh_three`() RETURNS varchar(255) CHARSET utf8
BEGIN
    DECLARE l_NEW_STR VARCHAR(255);  -- 定义变量
    DECLARE l_NAYIBU_NUM1   INT;
 
    SET l_NAYIBU_NUM1=101;

IF (l_NAYIBU_NUM1>100) THEN
       SET l_NEW_STR='TWO'; --赋值
    ELSE
 SET l_NEW_STR='ONE';
    END IF;
     RETURN(l_NEW_STR);--返回值
 
   END

2. 验证输出结果:

mysql> select sp_function_dbdh_three();
+---------------+
| sp_function_dbdh_three() |
+---------------+
| TWO           |
+---------------+
1 row in set (0.00 sec)