Wednesday, February 11, 2015

How to check if column exists in SQL Server table

SQL Server 2005 onwards:
if exists(select * from sys.columns 
            where Name = N'columnName' and Object_ID = Object_ID(N'tableName'))
begin
    -- Column Exists
end
And an UPPER CASE version:
IF EXISTS(SELECT * FROM sys.columns 
        WHERE [name] = N'columnName' AND [object_id] = OBJECT_ID(N'tableName'))
BEGIN
    -- Column Exists
END

No comments:

Post a Comment