DELIMITER $$ DROP FUNCTION IF EXISTS genPerson$$ CREATE FUNCTION genPerson(name varchar(20)) RETURNS varchar(50) BEGIN DECLARE str VARCHAR(50) DEFAULT ''; SET @tableName=name; SET str=CONCAT('create table ', @tableName,'(id int, name varchar(20));'); return str; END $$ DELIMITER ;
(2)DROP FUNCTION IF EXISTS genPerson$$ 如果函数genPerson已经存在了,就删除掉。
(3)CREATE FUNCTION 创建函数genPerson,函数的参数是name,返回值是varchar(50)。
(4)函数体放在BEGIN 与 END之间。
(5)DECLARE 声明变量,str类型是varchar(50),默认值是空。
(6)CONCAT连接多个字符串。
(7)RETURN 返回拼接后的字符串str。
DELIMITER $$ DROP FUNCTION IF EXISTS getNthHighestSalary$$ CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT BEGIN SET N = N -1; RETURN ( select ifnull((select distinct id from user order by id desc limit 1 OFFSET n),null) ); END
调用方法
select genPerson('student');
select getNthHighestSalary(2)
配置问题:
如果创建自定义函数时遇到下面这种报错:
Error Code : 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)
DELIMITER $$ DROP FUNCTION IF EXISTS fun_get_pick_loc_code$$ CREATE FUNCTION fun_get_pick_loc_code( schedulecode VARCHAR ( 100 ), materialnum VARCHAR ( 100 ), lotcode VARCHAR ( 100 ) ) RETURNS varchar(100) CHARSET utf8 BEGIN SET @num1 =( SELECT IFNULL( loc_code, "" ) FROM inventory_log WHERE inventory_type = 'delivery' AND order_type = 'picking' AND lot_code = lotcode AND material_num = materialnum AND order_num = ( SELECT preparation_order FROM preparation_master WHERE schedule_code = schedulecode AND preparation_type = '0' LIMIT 1 ) LIMIT 1 ); RETURN ( @num1 ); END $$ DELIMITER ;
调用
SELECT rd.return_order, rd.schedlule_code, rd.material_code, rd.lot_code, fun_get_pick_loc_code ( rd.schedlule_code, rd.material_code, rd.lot_code ) loc_code FROM return_detail rd WHERE rd.is_shelves =0
创建的各种函数例子(创建时都需要加分隔符和判断是否存在,以下省略了这个步骤,使用时注意看清楚)
1、fun_get_iqc_qty
CREATE DEFINER=`root`@`localhost` FUNCTION `fun_get_iqc_qty`(rdid int) RETURNS int(11) BEGIN RETURN (select FLOOR(IFNULL(sum(real_qty),0)) from receipt_detail_lot where receipt_det_id=rdid); END
2、fun_vendordate
CREATE DEFINER=`root`@`localhost` FUNCTION `fun_vendordate`(lotcode VARCHAR(50)) RETURNS varchar(50) CHARSET utf8 BEGIN RETURN (SELECT DATE_FORMAT(Input_Date,'%y%m%d') from a_iqc where Barcode=left(lotcode,16)); END
3、fun_vendorcode
CREATE DEFINER=`root`@`localhost` FUNCTION `fun_vendorcode`(lotcode VARCHAR(50)) RETURNS varchar(50) CHARSET utf8 BEGIN RETURN (SELECT Manufacturer from a_iqc where Barcode=left(lotcode,16)); END
4、fun_get_unit_qty
CREATE DEFINER=`root`@`localhost` FUNCTION `fun_get_unit_qty`(bomcode VARCHAR(50)) RETURNS float(22,9) BEGIN set @num1=IFNULL((select sum(use_qty) from bom_detail where bom_code=bomcode),0); set @num2=IFNULL((select sum(use_qty) from bom_replace where bom_code=bomcode),0); RETURN (@num1+@num2); END
5、fun_get_stack
CREATE DEFINER=`root`@`localhost` FUNCTION `fun_get_stack`(sch_code VARCHAR(30)) RETURNS varchar(100) CHARSET utf8 BEGIN set @num1=(select GROUP_CONCAT(distinct stack_code) from schedule_stack where schedule_code=sch_code); RETURN (@num1); END
6、fun_get_materialtype
CREATE DEFINER=`root`@`localhost` FUNCTION `fun_get_materialtype`(materialcode VARCHAR(50)) RETURNS varchar(30) CHARSET utf8 BEGIN if(left(materialcode,4)='AM31') THEN set @scode='SMT-PG'; else set @scode='SMT'; end if; RETURN @scode; END
7、fun_get_pick_qty
BEGIN if(schedulecode='') then SET @i= 99999; ELSE SET @i= (select IFNULL(sum(qty),0) from inventory_log where inventory_type='delivery' and order_type='picking' and material_num = materialcode and order_num = (select preparation_order from preparation_master where schedule_code=schedulecode and preparation_type='0' LIMIT 1 )); end if; RETURN (@i); END
8、fun_get_pick_loc_code
CREATE DEFINER=`root`@`localhost` FUNCTION `fun_get_pick_loc_code`( schedulecode VARCHAR ( 100 ), materialnum VARCHAR ( 100 ), lotcode VARCHAR ( 100 )) RETURNS varchar(100) CHARSET utf8 BEGIN SET @num1 =( SELECT IFNULL( loc_code, "" ) FROM inventory_log WHERE inventory_type = 'delivery' AND order_type = 'picking' AND lot_code = lotcode AND material_num = materialnum AND order_num = ( SELECT preparation_order FROM preparation_master WHERE schedule_code = schedulecode AND preparation_type = '0' LIMIT 1 ) LIMIT 1 ); RETURN ( @num1 ); END