我在将存储过程从 Firebird 转换为 MySQL 时遇到了麻烦。
以下是 Firebird 的现有代码:
SET TERM ^ ;
CREATE OR ALTER PROCEDURE "ActPerHour" (
days integer)
returns (
"DateHour" varchar(32),
"ActCount" integer)
as
declare variable "hours" integer;
declare variable "momentum" timestamp;
BEGIN
select cast('NOW' as timestamp) from rdb$database into :"momentum";
IF (:days > 31) THEN
EXCEPTION "max_a_month";
"hours" = :days*24;
WHILE (:"hours" >= 0) DO
BEGIN
select substring(addhour (:"momentum", -:"hours") from 1 for 13),
CASE
WHEN count(*) is NULL THEN 0
ELSE count(*)
END
from "Activity"
where substring(timestamp '01/01/1970' + "started" / 86400.00000 from 1 for 13) = substring(addhour (:"momentum", -:"hours") from 1 for 13)
and "started" > cast((:"momentum"-:days - timestamp '1970-01-01') * 86400 as integer)
into :"DateHour", :"ActCount";
"hours" = "hours" - 1;
SUSPEND;
END
END^
SET TERM ; ^
GRANT SELECT ON "Activity" TO PROCEDURE "ActPerHour";
GRANT EXECUTE ON PROCEDURE "ActPerHour" TO USER;
我在 MySQL 中尝试了以下操作:
DELIMITER $$
CREATE PROCEDURE ActPerHour (
IN days INT,
OUT DateHour VARCHAR(32),
OUT ActCount INT)
BEGIN
DECLARE hours INT;
DECLARE momentum DATETIME;
SELECT NOW() INTO momentum;
IF (days > 31) THEN
BEGIN
SELECT 'Maximum een maand' AS message;
END;
END IF;
set hours = days * 24;
WHILE (hours >= 0) DO
BEGIN
select (momentum - hours),
CASE
WHEN count(*) is NULL THEN 0
ELSE count(*)
END
from result
where started = (momentum - hours)
and started > momentum - days
into DateHour, ActCount;
set hours = hours - 1;
END;
END$$
DELIMITER ;
但我总是遇到 1064 错误。
错误代码:1064。您的 SQL 语法有错误;请查看与您的 MySQL 服务器版本相对应的手册,了解第 28 行“”附近使用的正确语法
我哪里错了?