这是代码
IF EXISTS(SELECT * FROM sys.columns
WHERE Name = 'columnName'
AND OBJECT_ID = OBJECT_ID('tableName') )
BEGIN
PRINT 'Your Column Exists'
END
我想知道如何转换此代码,它可以检查多列名称的存在。
答案 0 :(得分:11)
如果您正在测试是否所有列都在那里,您可以使用:
IF 3 = (select count(*) Names
from sys.columns
where OBJECT_ID = OBJECT_ID('tableName')
and Name in ('columnName1', 'columnName2', 'columnName3')
)
BEGIN
PRINT 'Your Columns Exist'
END
答案 1 :(得分:-6)
这个怎么样?
IF EXISTS (SELECT * FROM sys.columns
WHERE OBJECT_ID = OBJECT_ID('tableName')
AND( Name = 'columnName1' OR Name = 'columnName2' OR Name = 'columnName3')
)
BEGIN
PRINT 'Your Columns Exist'
END