如何执行用户定义的函数,以便数据库识别该函数已创建?
DELIMITER $$
CREATE FUNCTION `StudentGPA` (StudentID int, ClassStartDateStart datetime, ClassStartDateEnd date time) RETURNS decimal(3,2)
BEGIN
Declare AvgGPA decimal(3,2);
SELECT AvgGPA= isNULL(Avg(GPA), 0.0)
FROM Students_Classes
WHERE StudentID = StudentID
AND StartDate > ClassStartDateStart
AND StartDate <= ClassStartDateEnd
AND GPA > 0; RETURN AvgGPA;
END
答案 0 :(得分:0)
运行代码时,应该会出现语法错误。在您要指定参数的CREATE
语句中,您需要将ClassStartDateEnd date time
更改为ClassStartDateEnd datetime
。
DELIMITER $$
CREATE FUNCTION `StudentGPA`
(StudentID int,
ClassStartDateStart datetime,
ClassStartDateEnd datetime)
RETURNS decimal(3,2)
BEGIN
Declare AvgGPA decimal(3,2);
SELECT AvgGPA= isNULL(Avg(GPA), 0.0)
FROM Students_Classes
WHERE StudentID = StudentID
AND StartDate > ClassStartDateStart
AND StartDate <= ClassStartDateEnd
AND GPA > 0; RETURN AvgGPA;
END$$
DELIMITER ;
我在$$
语句之后使用了END
的DELIMITER来正确终止CREATE
,然后将分隔符设置回;
,这是一种很好的做法。< / p>