以下尝试创建一个UDF,它将找到某个列的第一个值并返回它,这给了我错误:
我做错了什么?这是SQL:
DELIMITER $$
CREATE FUNCTION getAppointment_DOB
(
_hsp integer,
_year integer,
_month integer,
_day integer,
_slot integer
)
RETURNS datetime
DETERMINISTIC
BEGIN
declare _answer datetime;
SELECT _answer = DOB
FROM `appointment`
where FK_HSP_ID = _hsp and (year(Start_Date) _year and
MONTH(Start_Date) = _month
and Day(Start_Date) = _day and K_Time_Slot_ID=_slot) limit 1,1;
RETURN _answer;
END;
$$ DELIMITER ;
答案 0 :(得分:-1)
你错过了=条件
的符号它不设置变量_answer,它返回一个带有列的集合 名为_answer
尝试使用INTO
语句作为您的选择。
你想要
DELIMITER $$
CREATE FUNCTION getAppointment_DOB
(
_hsp integer,
_year integer,
_month integer,
_day integer,
_slot integer
)
RETURNS datetime
DETERMINISTIC
BEGIN
declare _answer datetime;
SELECT DOB into _answer FROM `appointment` where FK_HSP_ID = _hsp and (year(Start_Date) = _year and MONTH(Start_Date) = _month and Day(Start_Date) = _day and K_Time_Slot_ID=_slot) limit 1,1;
RETURN _answer;
END;
$$ DELIMITER ;
看看这个Document