gpt4 book ai didi

mysql - mysql完美平方函数错误

转载 作者:行者123 更新时间:2023-11-29 12:31:43 24 4
gpt4 key购买 nike

这是我试图在 mysql 中创建的函数:

CREATE FUNCTION isPerfectSquare (testNum INT) 
RETURNS BOOLEAN
BEGIN
DECLARE sqrtint INT;
SET sqrtint = sqrt(testNum);
DECLARE result BOOLEAN DEFAULT FALSE;
IF sqrtint * sqrtint = testNum THEN
SET result = TRUE;
END IF;
RETURN result;
END;|

我将分隔符设置为 |,否则我根本无法走到这一步。 mysql 返回时出现以下错误:

DECLARE result BOOLEAN DEFAULT FALSE;

有人可以帮我看看我做错了什么吗?我刚刚编写了一个运行良好的不同函数,它以类似的方式运行,但我看不出这次我做了什么不同的事情导致了错误。

最佳答案

所有声明都必须位于 block 的开头:

CREATE FUNCTION isPerfectSquare (testNum INT) 
RETURNS BOOLEAN
BEGIN
DECLARE sqrtint INT;
DECLARE result BOOLEAN DEFAULT FALSE;
SET sqrtint = sqrt(testNum);
IF sqrtint * sqrtint = testNum THEN
SET result = TRUE;
END IF;
RETURN result;
END;|

来自documentation :

DECLARE is permitted only inside a BEGIN ... END compound statement and must be at its start, before any other statements.

关于mysql - mysql完美平方函数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27389588/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com