Created
April 5, 2016 12:35
-
-
Save obukhov/543637e1f86722b105c741c9c929ea9d to your computer and use it in GitHub Desktop.
Drop column stored procedure for mysql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DROP PROCEDURE IF EXISTS drop_column_if_exist; | |
DELIMITER ';;' | |
CREATE PROCEDURE drop_column_if_exist(IN schemaName CHAR(255), IN tableName CHAR(255), IN columnName CHAR(255)) BEGIN | |
IF exists( | |
SELECT * | |
FROM information_schema.columns | |
WHERE table_schema = schemaName AND table_name = tableName AND column_name = columnName) | |
THEN | |
SET @query = CONCAT('ALTER TABLE ', tableName, ' DROP COLUMN ', columnName, ';'); | |
PREPARE stmt FROM @query; | |
EXECUTE stmt; | |
DEALLOCATE PREPARE stmt; | |
END IF; | |
END;; | |
DELIMITER ';' | |
CALL drop_column_if_exist(SCHEMA(), 'table', 'column'); | |
DROP PROCEDURE IF EXISTS drop_column_if_exist; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment