ALTER TABLE user DROP COLUMN registered_date
我们可以使用上面的命令删除一列。但是,如果列registered_date存在,我怎么能放弃呢?
答案 0 :(得分:-1)
IF EXISTS(SELECT *
FROM information_schema.COLUMNS
WHERE
TABLE_SCHEMA = 'db_name'
AND TABLE_NAME = 'table_name'
AND COLUMN_NAME = 'column_name')
BEGIN
ALTER TABLE table_name DROP COLUMN column_name
END
或强>
IF (SELECT COUNT(*)
FROM information_schema.COLUMNS
WHERE
TABLE_SCHEMA = 'db_name'
AND TABLE_NAME = 'table_name'
AND COLUMN_NAME = 'column_name'>0)
BEGIN
ALTER TABLE table_name DROP COLUMN column_name
END