@a5635268
2015-09-23T08:20:43.000000Z
字数 994
阅读 1140
Mysql
用户自定义函数(user-defined function,UDF) 是一种对mysql的扩展途径,其用法与内置函数相同
语法
create function function_name
returns
{string|integer|real|decimal}
routine_body
Example1
delimiter $
create function f1(gdate datetime)
returns varchar(100) READS SQL DATA -- 一定要声明;
begin
declare x varchar(100) default '';
set x = date_format(gdate,'%Y年%m月%h日 %H时%m分%s秒');
return x;
end $
delimiter ;
Example2
delimiter $
create function str_cut(str varchar(255),length tinyint)
returns varchar(255) READS SQL DATA
begin
if(ISNULL(str)) then
return '';
elseif char_length(str) <= length then
return str;
elseif char_length(str) > length then
return contact(left(str,length),'...');
end if;
end $
delimiter ;
mysql> show function status\G
*************************** 1. row ***************************
Db: test
Name: f1
Type: FUNCTION
Definer: root@localhost
Modified: 2015-05-01 16:21:40
Created: 2015-05-01 16:21:40
Security_type: DEFINER
Comment:
character_set_client: utf8
collation_connection: utf8_general_ci
Database Collation: latin1_swedish_ci
mysql> drop function f1; -- f1 函数名;
Query OK, 0 rows affected