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')
- 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
- Code:
IF COL_LENGTH('schemaName.tableName', 'columnName') IS NOT NULL
BEGIN
-- Column Exists
END
Permissions in this forum:
You cannot reply to topics in this forum