4

我有一个引发错误的 MySQL 存储过程。

DELIMITER $$

DROP PROCEDURE IF EXISTS `test_schema`.`TEST_SPROC` $$  
CREATE PROCEDURE `test_schema`.`TEST_SPROC` (IN var0 INT, var1 INT)  
BEGIN

  DECLARE var0 INT;
  DECLARE var1 INT;

  SELECT COUNT(*) INTO var0 FROM INFORMATION_SCHEMA.`TABLES` 
  WHERE `TABLE_SCHEMA`='test_schema' AND `TABLE_NAME`='original_table'; 

  SELECT COUNT(*) INTO var1 FROM INFORMATION_SCHEMA.`COLUMNS` 
  WHERE `TABLE_SCHEMA`='test_schema' AND `TABLE_NAME`='new_table' 
  AND `COLUMN_NAME`='id';

  IF var0=1 THEN
    RENAME TABLE test_schema.original_table TO test_schema.new_table;   
  END IF;

  IF var1=1 THEN
    ALTER TABLE test_schema.new_table CHANGE id AccountID VARCHAR;   

  END IF;       #error is thrown here.

END $$

DELIMITER ;

错误:

脚本行:4 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以获取在 '; 附近使用的正确语法。万一;

END' 在第 15 行

我的 if 语句有什么问题?

4

2 回答 2

1

更改此行:

ALTER TABLE test_schema.new_table CHANGE id AccountID VARCHAR;

对此:

ALTER TABLE test_schema.new_table CHANGE id AccountID VARCHAR(100);

当然,您应该为VARCHAR列指定适当的长度。我只是用VARCHAR(100)了一个例子。

于 2013-07-09T10:01:42.343 回答
0

您必须为 alter table 语句指定长度。请参阅下面的评论:

IF varTable=1 THEN
    RENAME TABLE test_schema.original_table TO test_schema.new_table;
    SELECT COUNT(*) INTO varColumn FROM INFORMATION_SCHEMA.`COLUMNS` 
    WHERE `TABLE_SCHEMA`='test_schema' AND `TABLE_NAME`='new_table' 
    AND `COLUMN_NAME`='id';

    IF varColumn=1 THEN
      #The following statement must be "VARCHAR(255)"  not just "VARCHAR"
      ALTER TABLE test_schema.new_table CHANGE id AccountID VARCHAR(255);
    ELSE
      #statements.
    END IF;
END IF;
于 2013-12-07T05:22:09.230 回答