如何从表中删除列(如果存在) [英] how to drop column from table if exists

查看:187
本文介绍了如何从表中删除列(如果存在)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我正在使用的查询:

This is the query i am using:

IF  EXISTS(SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME =( N'CustomerVariable1Value'))
begin Alter table temp.DIM_BE_ACCOUNT  drop column CustomerVariable1Value

它第一次运行良好,但是当我再次运行它时,它显示错误. 如何使其无错误并执行多次?

It works fine the first time but when I run it again, it shows error. How to make it error free and executes it many number of times?

错误消息:

ALTER TABLE DROP COLUMN failed because column 'CustomerVariable1Value' does not exist in table 'DIM_BE_ACCOUNT'.

推荐答案

您仅在整个MySQL实例的所有列名称中寻找一个列名称.您还需要按架构(=数据库)和表名进行过滤:

You are only looking for a column name out of all column names in the entire MySQL instance. You need to also filter by schema (=database) and table names:

IF EXISTS(SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME = N'CustomerVariable1Value' AND TABLE_NAME = 'MyTableName' AND TABLE_SCHEMA = 'MyDatabase')

这篇关于如何从表中删除列(如果存在)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆