terrydiederich2
1/10/2019 - 2:50 PM

Add Column only if it doesn't already exist

select if (
    (
    SELECT COUNT(1) ColumnIsThere
      FROM INFORMATION_SCHEMA.STATISTICS
     WHERE table_schema=DATABASE() 
       AND table_name='shiplocations' 
       AND COLUMN_NAME='PlantID'
    ) > 0
    ,'select 1;'
    ,'ALTER TABLE shiplocations ADD COLUMN PlantID INT(11) NOT NULL DEFAULT 0;') INTO @tmp;
PREPARE stmt1 FROM @tmp;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;