我习惯在 MS SQL Server 中工作,所以 MySQL 对我来说总是有点陌生。我为 MS SQL Server 编写了以下内容,并尝试将其移植到 MySQL:
CREATE FUNCTION ToRadians
(@Degrees float)
RETURNS float
AS
BEGIN
DECLARE @Radians float
SELECT @Radians = @Degrees * 3.1415927 / 180
RETURN @Radians
END
GO
我有
CREATE FUNCTION ToRadians
(@Degrees float)
RETURNS float
BEGIN
DECLARE @Radians float;
SELECT @Radians = @Degrees * 3.1415927 / 180;
RETURN @Radians;
END
但是在 PHPMyAdmin 中,这给了我错误:
Error
SQL query:
CREATE FUNCTION ToRadians(
@Degrees float
) RETURNS float AS BEGIN DECLARE@Radians float;
MySQL said: Documentation
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '@Degrees float)
RETURNS float
AS
BEGIN
DECLARE @Radians float' at line 2
我所做的搜索表明上述 MySQL UDF 应该是正确的,但显然不是。 SELECT version() 返回 5.0.77
最佳答案
CREATE FUNCTION ToRadians (in_degrees FLOAT) RETURNS FLOAT
DETERMINISTIC
BEGIN
RETURN in_degrees * 3.1415927 / 180;
END;
确定性,因为给定度数的返回值应始终以弧度表示相同。
此外,DEGREES() 和 RADIANS() 函数已经存在于 mysql 中。 RADIANS()
关于mysql - 在 MySQL 中创建函数时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8123117/