语法结构:
CREATE
[DEFINER = user]
FUNCTION [IF NOT EXISTS] fun_name ([func_parameter[,...]])
RETURNS type
[characteristic ...] routine_body
举例说明:
CREATE FUNCTION addvalue(num1 BIGINT, num2 BIGINT)
RETURNS BIGINT
BEGIN
RETURN num1+num2;
END
客户端设置:
SET GLOBAL log_bin_trust_function_creators = 1;
如果没有这个客户端设置,在创建函数的时候,会产生如下报警:
1418 - This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration and binary logging is enabled (you *might* want to use the less safe log_bin_trust_function_creators variable)
使用举例:
#定义一个函数
CREATE DEFINER = CURRENT_USER FUNCTION addvalue ( num1 BIGINT, num2 BIGINT ) RETURNS BIGINT BEGIN
RETURN num1 + num2;
END
#使用一个函数
SELECT addvalue(1,2);