I'm looking to add a column only if it does not already exist before. The motivation for this is that we can upgrade any version of a production instance to the latest version.
This is what I'm trying, but I keep getting a syntax error near the IF
statement:
use database_name
SELECT @rowcount:=COUNT(column_name)
FROM information_schema.columns
WHERE table_name = 'table_name'
AND column_name = 'column_2';
IF @rowcount < 1 THEN
ALTER TABLE table_name
ADD COLUMN column_2 VARCHAR(42) DEFAULT 'abcd',
END IF;
commit;
What am I doing wrong?