tutorial net
*********WOOOOOOW *********
We're happy to see you're enjoying our races (already 5 pages viewed today)! You can keep checking out by becoming a member of the tutorial net community. It's free!
You will also be able to keep track of your race progress, practice on exercises, and chat with other members.

Join the forum, it's quick and easy

tutorial net
*********WOOOOOOW *********
We're happy to see you're enjoying our races (already 5 pages viewed today)! You can keep checking out by becoming a member of the tutorial net community. It's free!
You will also be able to keep track of your race progress, practice on exercises, and chat with other members.
tutorial net
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Go down
avatar
Admin
Admin
Posts : 207
Join date : 2017-11-11
Age : 33
Location : algeria
http://www.tutorial-net.com

SQL Server - How to check if a column exists in a SQL Server table? Empty SQL Server - How to check if a column exists in a SQL Server table?

Wed Apr 11, 2018 5:24 pm
I need to add a specific column if it is does not exist. I have something like this, but it always returns false:
Code:
IF EXISTS(SELECT *
          FROM  INFORMATION_SCHEMA.COLUMNS
          WHERE  TABLE_NAME = 'myTableName'
                AND COLUMN_NAME = 'myColumnName')
Answers
Code:
SQL Server 2005 onwards:
IF EXISTS(SELECT 1 FROM sys.columns
          WHERE Name = N'columnName'
          AND Object_ID = Object_ID(N'schemaName.tableName'))
BEGIN
    -- Column Exists
END
Martin Smith's version is shorter:
Code:
IF COL_LENGTH('schemaName.tableName', 'columnName') IS NOT NULL
BEGIN
    -- Column Exists
END
Back to top
Permissions in this forum:
You cannot reply to topics in this forum