Oct 2, 2015

How to check if column exists in SQL Server table


Following Query may help you to check existing column in SQL server.


IF EXISTS( 
            SELECT Name 
            FROM sys.columns

            WHERE Name = N'Col_Name'  
            AND Object_ID = Object_ID(N'Tbl_Name')
)

BEGIN

    print 'true'

END


Note: It will return NULL if the user doesn't have permissions to view the metadata because sys.columns checks user access too.