How do I select a value from a column only if the column exists?
Posted on 2008-10-14
I need to develop a DDL that retrieves a value from a column of a table. However, when the DDL is executed on some databases, the column may or may not exist. My below code is not working:
DECLARE @TSN VARCHAR(50) --to store theater server url from THEATER_LK
SET @TSN = NULL
IF EXISTS (SELECT TABLE_NAME, COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = N'THEATER_LK' AND COLUMN_NAME = N'DTHEATER_SERVER_NAME')
SELECT @TSN=DTHEATER_SERVER_NAME FROM THEATER_LK
This returns the following error when the column does not exist. What's wrong with my syntax?
"Invalid column name 'DTHEATER_SERVER_NAME'