mugyu
8/25/2014 - 5:18 AM

mysqlでカラムが存在すれば DROP COLUMN するスクリプト

mysqlでカラムが存在すれば DROP COLUMN するスクリプト

DELIMITER //
CREATE PROCEDURE drop_column_if_exists()
BEGIN

IF EXISTS (
  SELECT column_name
  FROM information_schema.columns
  WHERE
    table_schema = 'sail' AND
    table_name   = '@table_name' AND
    column_name  = '@column_name'
) THEN
  ALTER TABLE `@table_name` DROP COLUMN `@column_name`;
END IF;

END//
DELIMITER ;

CALL drop_column_if_exists();
DROP PROCEDURE IF EXISTS drop_column_if_exists;

-- s/@table_name/target_table_name/g
-- s/@column_name/target_column_name/g