如何修复过程SQL中的错误?

时间:2014-10-20 19:34:02

标签: mysql sql stored-procedures

有一个hranimka,当它被创建时,会发生错误。也许她受到了......的打击。

存储过程:

   CREATE PROCEDURE insert_log(

  IN LogType INT, 
  IN LogIdNote INT,
  IN LogName VARCHAR, 
  IN LogTime TIMESTAMP, 
  IN logTypeCategory INT, 
  IN LogIdUser INT) 

begin

INSERT INTO log (LogType, 
                 LogIdNote, 
                 LogName, 
                 LogTime, 
                 logTypeCategory, 
                 LogIdUser, 
                 LogTypeUser, 
                 LogUrl)
SELECT LogType, LogIdNote, LogName, LogTime, logTypeCategory, LogIdUser, url.URLCategorysubscribetotype, u.UsersTypeAccount FROM users u LEFT JOIN categorysubscribetotype url ON url.CategoryTypeCategorysubscribetotype = LogType WHERE u.idUsers = LogIdUser;

end //

错误:

  

1064 - 您的SQL语法出错;检查手册   对应于您的MySQL服务器版本,以便使用正确的语法   'INT LogType,INT LogIdNote,VARCHAR LogName,TIMESTAMP LogTime附近,   我在第3行

我只尝试在params更改数据类型。

我认为,下一个代码会给我一个很好的结果,但是我需要在变量的SELECT查询中保存结果并将其插入查询Insert:

DELIMITER |

CREATE PROCEDURE insert_log(

  IN pLogType INT, 
  IN pLogIdNote INT,
  IN pLogName VARCHAR(150), 
  IN pLogTime TIMESTAMP, 
  IN plogTypeCategory INT, 
  IN pLogIdUser INT) 

BEGIN

DECLARE user_type INT DEFAULT 0;
DECLARE url VARCHAR(250) DEFAULT;

SET user_type = (SELECT UsersTypeAccount FROM users WHERE idUsers = pLogIdUser);
SET url = (SELECT URLCategorysubscribetotype FROM categorysubscribetotype WHERE CategoryTypeCategorysubscribetotype = pLogType);

INSERT INTO log (pLogType, 
                 pLogIdNote, 
                 pLogName, 
                 pLogTime, 
                 plogTypeCategory, 
                 pLogIdUser, 
                 pLogTypeUser, 
                 pLogUrl)
VALUES (
                 LogType, 
                 LogIdNote, 
                 LogName, 
                 LogTime, 
                 logTypeCategory, 
                 LogIdUser, 
                 user_type, 
                 url
);

END |

delimiter ;

2 个答案:

答案 0 :(得分:0)

Your syntax is wrong。数据类型位于参数名称之后,IN / OUT说明符之前。像这样:

CREATE PROCEDURE insert_log(

  IN LogType INT, 
  IN LogIdNote INT,
  IN LogName VARCHAR(10), 
  IN LogTime TIMESTAMP, 
  IN logTypeCategory INT, 
  IN LogIdUser INT)

begin

...

修改:另请注意,自it requires one以来,我向VARCHAR数据类型添加了大小说明符。我猜到10,但你可以用你的任何东西取代那个值。

答案 1 :(得分:0)

您的问题在这里:

INSERT INTO log (pLogType, //wrong!
                 pLogIdNote, 
                 pLogName, 
                 pLogTime, 
                 plogTypeCategory, 
                 pLogIdUser, 
                 pLogTypeUser, 
                 pLogUrl)

您已将参数用作列,而VALUES尝试此查询

DELIMITER //
 CREATE PROCEDURE insert_log(
    IN pLogType INT, 
    IN pLogIdNote INT,
    IN pLogName VARCHAR(150), 
    IN pLogTime TIMESTAMP, 
    IN plogTypeCategory INT, 
    IN pLogIdUser INT) 

   BEGIN

    DECLARE user_type INT DEFAULT 0;
    DECLARE url VARCHAR(250) DEFAULT;

    SET user_type = (
                        SELECT UsersTypeAccount 
                        FROM users 
                        WHERE idUsers = pLogIdUser
                    );
    SET url = (
                        SELECT URLCategorysubscribetotype 
                        FROM categorysubscribetotype 
                        WHERE CategoryTypeCategorysubscribetotype = pLogType
               );

    INSERT INTO log (
                     `LogType`, 
                     `LogIdNote`, 
                     `LogName`, 
                     `LogTime`, 
                     `logTypeCategory`, 
                     `LogIdUser`, 
                     `LogIdUserType`, /*I added this*/
                     `LogIdUrl`,     /*this one too */
    )VALUES (
                     pLogType, 
                     pLogIdNote, 
                     pLogName, 
                     pLogTime, 
                     plogTypeCategory,  
                     pLogIdUser,
                     user_type, 
                     url
    );

   END //
 DELIMITER ;

请注意您需要调整此存储过程,几乎没有错误。例如,pLogTypeUserpLogUrl未定义,我在需要更改列名的地方添加了注释。