33

I'm using a SQL server statement embedded in some other C# code; and simply want to check if a column exists in my table.

If the column (ModifiedByUSer here) does exist then I want to return a 1 or a true; if it doesn't then I want to return a 0 or a false (or something similar that can be interpreted in C#).

I've got as far as using a CASE statement like the following:

SELECT cast(case WHEN EXISTS (select ModifiedByUser from Tags) 
            THEN 0 
            ELSE 1 
            END as bit)

But if the ModifiedByUser doesn't exist then I'm getting an invalid column name, instead of the return value.

I've also considered:

IF EXISTS(SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'Tags' AND COLUMN_NAME = 'ModifiedByUser')
  BEGIN // Do something here to return a value
  END

But don't know how to conditionally return a value/bool/bit based on the result. Any help much appreciated!

1
  • As you've discovered, it's better to add an answer below the question, to maintain the standard question-answer format. Thanks for adding it! Commented Jan 23, 2014 at 9:24

5 Answers 5

55

Final answer was a combination of two of the above (I've upvoted both to show my appreciation!):

select case 
   when exists (
      SELECT 1 
      FROM Sys.columns c 
      WHERE c.[object_id] = OBJECT_ID('dbo.Tags') 
         AND c.name = 'ModifiedByUserId'
   ) 
   then 1 
   else 0 
end
Sign up to request clarification or add additional context in comments.

1 Comment

Since the original post mentioned using something like C# to work with the result, I would suggest also casting the value to a bit in the sql. That way C# can actually work with the returned value as a bool.
16
select case
         when exists (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'Tags' AND COLUMN_NAME = 'ModifiedByUser')
         then 0
         else 1
       end

2 Comments

Thanks for the answer, but it seems to be always returning 1, whether the column is present or not.
I've combined your answer with the one below and it seems to work: select case when exists (SELECT 1 FROM Sys.columns c WHERE c.[object_id] = OBJECT_ID('dbo.Tags') AND c.name = 'ModifiedByUserId') then 1 else 0 end
3
SELECT *
FROM ...
WHERE EXISTS(SELECT 1 
        FROM sys.columns c
        WHERE c.[object_id] = OBJECT_ID('dbo.Tags')
            AND c.name = 'ModifiedByUser'
    )

Comments

2

Try this one -

SELECT *
FROM ...
WHERE EXISTS(SELECT 1 
        FROM sys.columns c
        WHERE c.[object_id] = OBJECT_ID('dbo.Tags')
            AND c.name = 'ModifiedByUser'
    )

3 Comments

@table_name like 'dbo.table1'
How do I use this? Sorry not familiar with SQL and this is hard to get my head around.
I've combined your answer with the one above and it seems to work: select case when exists (SELECT 1 FROM Sys.columns c WHERE c.[object_id] = OBJECT_ID('dbo.Tags') AND c.name = 'ModifiedByUserId') then 1 else 0 end
1

You can check in the system 'table column mapping' table

SELECT count(*)
  FROM Sys.Columns c
  JOIN Sys.Tables t ON c.Object_Id = t.Object_Id
 WHERE upper(t.Name) = 'TAGS'
   AND upper(c.NAME) = 'MODIFIEDBYUSER'

2 Comments

Thanks but this is giving me an Invalid column name Table_Name for line 4. Any ideas why?
@WheretheresaWill .. My Bad.. I have change and corrected the query.. now check

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.