1

i'm asking this question after googling with no luck. it has been asked before on SOF but that's long ago MySQL Alter syntax to drop a column if it exists. Hope things have changed since.

Is there a straight forward way to drop a column in table if it exists.

I'm using MySql 5.6, i would like to wrap this alter statement around an if to avoid any issue if the script runs twice:

ALTER TABLE xyz DROP COLUMN abc, ADD COLUMN ghi DATE NOT NULL AFTER column4;

Thanks

Community
  • 1
  • 1
Adil H. Raza
  • 1,649
  • 20
  • 25
  • Possible duplicate of [MySQL Alter syntax to drop a column if it exists](https://stackoverflow.com/questions/173814/mysql-alter-syntax-to-drop-a-column-if-it-exists) – Matthias Winkelmann Aug 28 '17 at 17:40

1 Answers1

0

You may check if column exists and after perform the other operations.

See here: MySQL, Check if a column exists in a table with SQL

Community
  • 1
  • 1
Lipsyor
  • 428
  • 1
  • 7
  • 20
  • I tried that also, but in my workbench it gives an error saying Syntax error: 'IF' (if) is not valid input at this position. i'm trying this in "new sql tab for executing queries". – Adil H. Raza Nov 20 '15 at 13:12
  • ok i can't do that, my apologies. it has to be in stored procedure. – Adil H. Raza Nov 20 '15 at 13:25