2015-06-08 27 views
2

我已经开始使用MySQL创建存储过程。 然后我想进行迁移:up(MyBatis)。SQL语法错误。创建存储过程

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 

之后迁移:最高命令执行

我收到一个错误

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 
+0

你正在使用奇单引号。删除它们并使用标准单引号'而不是' – CathalMF

+0

毫无疑问,您正在收到语法错误。您的存储过程是在许多地方使用SQL Sever语法编写的。 –

回答

2

我解决了这个问题。最终的代码如下所示:

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;

2

你应该改变 '分隔符'

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 ; 
+0

我加了DELIMITER //,DELIMITER; 。所以现在错误是:原因:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:你的SQL语法有错误;检查与您的MySQL服务器版本相对应的手册,以便在'delimiter'附近使用正确的语法'CREATE DEFINER ='root' @'localhost' PROCEDURE'add_tips'(g'at line 1 – Dylan