SQL 语法错误。创建存储过程
SQL syntax error. Creating of Stored Procedure
我已经开始使用 MySQL 创建存储过程。
然后我想制作 migration:up (MyBatis)。
mvn migration:up -Dmigration.path=/path/to/repository
这是我的存储过程
DROP PROCEDURE IF EXISTS add_tips;
CREATE DEFINER=`root`@`localhost` PROCEDURE `add_tips`(gspId INTEGER, gameID INTEGER)
BEGIN
DECLARE @start_datetime = getdate();
DECLARE @execution_time_in_seconds int;
DECLARE @LID int;
INSERT INTO sp_logs(spName, startTime) VALUES(`add_tips`, @start_datetime);
SET @LID = LAST_INSERT_ID();
...
/*some code goes here*/
...
@execution_time_in_seconds = datediff(SECOND,@start_datetime,getdate())
UPDATE sp_logs
SET executionTime = @execution_time_in_seconds
WHERE logId = @LID;
END
之后migration:up命令执行
我收到一个错误
Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:
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 '@start_datetime = getdate();
[INFO] DECLARE @execution_time_in_seconds int;
[INFO] DECLARE @LI' at line 4
你应该改变 'delimiter'
DROP PROCEDURE IF EXISTS add_tips;
delimiter //
CREATE DEFINER=`root`@`localhost` PROCEDURE `add_tips`(gspId INTEGER, gameID INTEGER)
BEGIN
DECLARE @start_datetime = getdate();
DECLARE @execution_time_in_seconds int;
DECLARE @LID int;
INSERT INTO sp_logs(spName, startTime) VALUES(`add_tips`, @start_datetime);
SET @LID = LAST_INSERT_ID();
...
/*some code goes here*/
...
@execution_time_in_seconds = datediff(SECOND,@start_datetime,getdate())
UPDATE sp_logs
SET executionTime = @execution_time_in_seconds
WHERE logId = @LID;
END //
delimiter ;
我解决了这个问题。最终代码如下所示:
DROP PROCEDURE IF EXISTS add_tips;
CREATE DEFINER=`root`@`localhost` PROCEDURE `add_tips`(gspId INTEGER, gameID INTEGER)
BEGIN
DECLARE start_datetime DATETIME;
DECLARE execution_time TIME;
DECLARE lid INTEGER;
SET start_datetime = NOW();
INSERT INTO sp_logs(spName,startTime) values('add_tips', start_datetime);
SET lid = LAST_INSERT_ID();
...
/*some code goes here*/
...
SET execution_time = TIMEDIFF(NOW(), start_datetime);
UPDATE sp_logs
SET executionTime = execution_time
WHERE logId = lid;
END;
问题是END;
我已经开始使用 MySQL 创建存储过程。 然后我想制作 migration:up (MyBatis)。
mvn migration:up -Dmigration.path=/path/to/repository
这是我的存储过程
DROP PROCEDURE IF EXISTS add_tips;
CREATE DEFINER=`root`@`localhost` PROCEDURE `add_tips`(gspId INTEGER, gameID INTEGER)
BEGIN
DECLARE @start_datetime = getdate();
DECLARE @execution_time_in_seconds int;
DECLARE @LID int;
INSERT INTO sp_logs(spName, startTime) VALUES(`add_tips`, @start_datetime);
SET @LID = LAST_INSERT_ID();
...
/*some code goes here*/
...
@execution_time_in_seconds = datediff(SECOND,@start_datetime,getdate())
UPDATE sp_logs
SET executionTime = @execution_time_in_seconds
WHERE logId = @LID;
END
之后migration:up命令执行
我收到一个错误
Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:
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 '@start_datetime = getdate();
[INFO] DECLARE @execution_time_in_seconds int;
[INFO] DECLARE @LI' at line 4
你应该改变 'delimiter'
DROP PROCEDURE IF EXISTS add_tips;
delimiter //
CREATE DEFINER=`root`@`localhost` PROCEDURE `add_tips`(gspId INTEGER, gameID INTEGER)
BEGIN
DECLARE @start_datetime = getdate();
DECLARE @execution_time_in_seconds int;
DECLARE @LID int;
INSERT INTO sp_logs(spName, startTime) VALUES(`add_tips`, @start_datetime);
SET @LID = LAST_INSERT_ID();
...
/*some code goes here*/
...
@execution_time_in_seconds = datediff(SECOND,@start_datetime,getdate())
UPDATE sp_logs
SET executionTime = @execution_time_in_seconds
WHERE logId = @LID;
END //
delimiter ;
我解决了这个问题。最终代码如下所示:
DROP PROCEDURE IF EXISTS add_tips;
CREATE DEFINER=`root`@`localhost` PROCEDURE `add_tips`(gspId INTEGER, gameID INTEGER)
BEGIN
DECLARE start_datetime DATETIME;
DECLARE execution_time TIME;
DECLARE lid INTEGER;
SET start_datetime = NOW();
INSERT INTO sp_logs(spName,startTime) values('add_tips', start_datetime);
SET lid = LAST_INSERT_ID();
...
/*some code goes here*/
...
SET execution_time = TIMEDIFF(NOW(), start_datetime);
UPDATE sp_logs
SET executionTime = execution_time
WHERE logId = lid;
END;
问题是END;