MySQL Forums
Forum List  »  General

Add column if missing
Posted by: Dan Kelly
Date: July 21, 2014 08:39AM

I am writing a maintenance application for our database (MySQL 5.1) and would like to check that a column exists before I try and add it to a table.

The Statement I am trying to use to check before updating is as follows:

IF NOT EXISTS(SELECT *
FROM information_schema.COLUMNS
WHERE table_schema = "my_schema"
AND table_name = "my_table"
AND column_name = "new_column" ) THEN
ALTER TABLE my_table
ADD COLUMN `new_column` INT(10) UNSIGNED NULL;
END IF

The SELECT works on it's own, as does the ALTER TABLE

Any Idea why the whole doesn't?

Options: ReplyQuote


Subject
Written By
Posted
Add column if missing
July 21, 2014 08:39AM
August 18, 2014 01:20AM
August 29, 2014 05:28AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.