4

I have a MySQL stored procedure that throws an error.

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 ;

Error:

Script line: 4 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 '; END IF;

END' at line 15

What is wrong with my if statement?

1
  • Your if statement is correctly formed. It's the alter table statement that has syntax errors. Commented Dec 7, 2013 at 5:24

2 Answers 2

1

Change this line:

ALTER TABLE test_schema.new_table CHANGE id AccountID VARCHAR;

to this:

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

Of course you should specify a length for the VARCHAR column that is appropriate. I've just used VARCHAR(100) as an example.

Sign up to request clarification or add additional context in comments.

Comments

0

You have to specify a length for the alter table statement. See the comment below:

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.