1.错误写法:
CREATE FUNCTION factorial1(n DECIMAL(3,0)) RETURN DECIMAL(20,0)
DETERMINISTIC
BEGIN
DECLARE factorial DECIMAL(20,0) DEFAULT 1;
DECLARE counter DECIMAL(3,0);
SET counter=n;
factorial_loop:REPEAT
SET factorial=factorial*counter;
SET counter=counter-1;
UNTIL counter=1
END REPEAT;
RETURN factorial;
end;
2.正确写法:
CREATE FUNCTION factorial1(n DECIMAL(3,0)) RETURNS DECIMAL(20,0)
DETERMINISTIC
BEGIN
DECLARE factorial DECIMAL(20,0) DEFAULT 1;
DECLARE counter DECIMAL(3,0);
SET counter=n;
factorial_loop:REPEAT
SET factorial=factorial*counter;
SET counter=counter-1;
UNTIL counter=1
END REPEAT;
RETURN factorial;
end;
区别解释:
这两段代码的区别就在于前者在返回值的时候使用的是 RETURN 而后者使用的是 RETURNS。