CustomerVariable1Value

CustomerVariable1Value

这是我正在使用的查询:

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')

08-18 10:47