3

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.

5
  • Change equal operator to IN operator then Name, some like this: SELECT * FROM sys.columns WHERE Name IN ('columnName_1','columnName_2') AND OBJECT_ID = OBJECT_ID('tableName'). Commented Apr 15, 2015 at 3:33
  • like this -- where name in 'columnName' ?? Commented Apr 15, 2015 at 3:36
  • like this: WHERE Name IN ('columnName_1','columnName_2'). Commented Apr 15, 2015 at 3:38
  • @JuanRuizdeCastilla: IN clause will fail in this condition, evn if any one of the column in the IN Clause does not exists, result will still show your columns exists. Please recheck this. Commented Apr 15, 2015 at 3:42
  • you right, here put the correct answer. Commented Apr 15, 2015 at 3:54

3 Answers 3

13

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  
Sign up to request clarification or add additional context in comments.

5 Comments

Yes, this will work. Only thing is hardcoded value '3'.
True, but you also hardcoded the column names as well. A more generic solution would have a table of column names that your searching for... or a string list... but then the solution is a bit more complicated. If you need it more generic... how would you like to specify the list of columns?
Yes, That's why given +1 to you.
thanks for the answer! but how if the columns come from different tables?
If you want to check fields in multiple tables, just do an equality check in the if statement for each table joined by AND. Like this: if 3 = (select count()....for fields in table1) AND 2=(Select count()...for fields in table 2) AND... etc.
-1

something similar but easier to understand would be

IF EXISTS 
    (
      SELECT *
      FROM INFORMATION_SCHEMA.COLUMNS
      WHERE TABLE_NAME = 'Table Name'
      AND(COLUMN_NAME = 'column 1'
      or COLUMN_NAME = 'column 2'
      or COLUMN_NAME = 'column 3'
      or COLUMN_NAME = 'column 4')
    )
    SELECT 'Column exists in table' AS[Status];
    ELSE
    SELECT 'Column does not exist in table' AS[Status];

1 Comment

This answer has the same issue as the one below. It returns true if only one column is present instead of all columns.
-8

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 

1 Comment

The difference between this one and mine is that this one will report that the columns exist if any one of them exist. My code will only return that the columns exist if all of them exist.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.