如何检查SQL Server中是否存在多个列

时间:2023-02-08 07:12:12

Here is the code

这是代码

IF EXISTS(SELECT * FROM sys.columns
          WHERE Name = 'columnName' 
            AND OBJECT_ID = OBJECT_ID('tableName') )
BEGIN
    PRINT 'Your Column Exists'
END  

I want to know how to convert this code that it can check multiple column name existence.

我想知道如何转换这个代码,它可以检查多个列名的存在。

2 个解决方案

#1


-3  

How about this?

这个怎么样?

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 

#2


8  

If you're testing if all the columns are there you could use:

如果你正在测试是否所有列都在那里,你可以使用:

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


-3  

How about this?

这个怎么样?

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 

#2


8  

If you're testing if all the columns are there you could use:

如果你正在测试是否所有列都在那里,你可以使用:

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