如何检查SQLServer表中是否存在列?我需要添加一个特定的列,如果它不存在。我有如下内容,但它总是返回false:IF EXISTS(SELECT *
FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'myTableName'
AND COLUMN_NAME = 'myColumnName')如何检查SQLServer数据库的表中是否存在列?
3 回答
潇潇雨雨
TA贡献1833条经验 获得超4个赞
IF EXISTS(SELECT 1 FROM sys.columns WHERE Name = N'columnName' AND Object_ID = Object_ID(N'schemaName.tableName'))BEGIN -- Column ExistsEND
IF COL_LENGTH('schemaName.tableName', 'columnName') IS NOT NULLBEGIN -- Column ExistsEND
函数式编程
TA贡献1807条经验 获得超9个赞
IF COL_LENGTH('table_name','column_name') IS NULLBEGIN/* Column does not exist or caller does not have permission to view the object */END
COL_LENGTH
COL_LENGTH('AdventureWorks2012.HumanResources.Department','ModifiedDate')
COL_LENGTH
胡子哥哥
TA贡献1825条经验 获得超6个赞
if not exists (select column_name from INFORMATION_SCHEMA.columns where table_name = 'MyTable' and column_name = 'MyColumn') alter table MyTable add MyColumn int
编辑以处理编辑到问题
添加回答
举报
0/150
提交
取消