这是我正在使用的查询:
IF EXISTS(SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME =( N'CustomerVariable1Value')) begin Alter table temp.DIM_BE_ACCOUNT drop column CustomerVariable1Value
第一次运行正常,但是当我再次运行它时,显示错误。如何使其无错误并执行多次?
错误信息:
ALTER TABLE DROP COLUMN failed because column 'CustomerVariable1Value' does not exist in table 'DIM_BE_ACCOUNT'.
您只在整个MySQL实例的所有列名中寻找一个列名。您还需要按架构(=数据库)和表名进行过滤:
IF EXISTS(SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME = N'CustomerVariable1Value' AND TABLE_NAME = 'MyTableName' AND TABLE_SCHEMA = 'MyDatabase')