SQL语法错误。创建存储过程

时间:2015-06-08 12:04:40

标签: mysql sql stored-procedures mybatis

我已经开始使用MySQL创建存储过程。 然后我想进行迁移: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

在迁移之后:执行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

2 个答案:

答案 0 :(得分: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 ;

答案 1 :(得分: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;