简体   繁体   English

MySQL存储过程1064错误

[英]MySQL stored procedure 1064 Error

For the life of me, I can't figure out why I'm getting this error: 对于我的一生,我无法弄清楚为什么会出现此错误:

[Err] 1064 - You have an error in your SQL syntax; [Err] 1064-您的SQL语法有误; check the manual that corresponds to your MySQL server version for the right syntax to use near 'NULL' at line 1 检查与您的MySQL服务器版本相对应的手册,以在第1行的'NULL'附近使用正确的语法

I'm calling a simple stored procedure with some variables passed in. What you don't see here is the setting of the delimiter to //, but that's done as well. 我正在调用一个简单的存储过程,并传入一些变量。您在这里看不到的是将分隔符设置为//,但这也完成了。

Thanks much! 非常感谢!

SET @dest_database                   = 'my_db';
SET @table_category_management_pages = 'category_management_pages';


 CREATE PROCEDURE category_management_pages(
    IN in_name varchar(255),
    IN in_display varchar(255),
    IN in_notes varchar(255),
    IN in_order INT,
    IN in_title varchar(255),
    IN in_access_name varchar(255)
  )
  BEGIN
    IF NOT EXISTS(
      SELECT * FROM information_schema.COLUMNS
      WHERE
      TABLE_SCHEMA = @dest_database AND
      TABLE_NAME   = @table_category_management_pages AND
      COLUMN_NAME  = 'key_hash'
    ) THEN
      # Add UNIQUE index on key_hash
      SET @myPrep = CONCAT('ALTER TABLE `', @dest_database, '`.`', @table_category_management_pages, '` ADD COLUMN `key_hash` varchar(255) NULL, ADD UNIQUE INDEX (`key_hash`);');
      prepare stmt from @myPrep;
      execute stmt;
    END IF;

    # Update key_hash to latest hash
    SET @myPrep = CONCAT('UPDATE `', @dest_database, '`.`', @table_category_management_pages, '` 
      SET `key_hash` = md5(`name`)');
     prepare stmt from @myPrep;
     execute stmt;

END//

I'm answering my own question because I found out why the prepared statement wasn't working and it is something really simple dumb, but may affect others in the future. 我之所以回答自己的问题,是因为我发现了为什么准备好的语句不起作用,这确实很愚蠢,但是将来可能会影响其他人。 At our company, we have a habit of lining up all the equal signs when declaring variables (see edits above). 在我们公司,我们习惯于在声明变量时排所有等号(请参见上面的编辑)。 Well, it seems like MySQL doesn't like whitespace in this manner, so the global variables weren't being passed in the prepared statement correctly. 好吧,似乎MySQL不喜欢这种方式的空格,因此全局变量未在准备好的语句中正确传递。 By changing the declaration to this: 通过将声明更改为此:

/* Globals */
SET @dest_database = 'my_db';
SET @table_category_management_pages = 'category_management_pages';

...worked out fine. ...工作得很好。 Go figure. 去搞清楚。 Both suggestion in the comments were good, but in the end it was something due to whitespace. 评论中的两个建议都很好,但最后是由于空白。 Very frustrating, but I hope this helps someone in the future. 非常令人沮丧,但是我希望这对将来的人有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM