Is it possible to verify the existence of a column inside an inline operator? Note that this is not a duplicate of the general question, "How to check if a column exists." This refers to a possible INLINE solution.
Is there a way to do this, but since it needs to be written for the BCP [out] command, I can’t use SP, and the compatibility must match MSSQL 2000.
Could there be something similar to the following:
SELECT
[col1],
[col2],
CASE
WHEN (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'Table' AND COLUMN_NAME = 'col3') = 1
THEN [col3]
ELSE null
END
FROM [MyDb].[dbo].[Table]
or a sub-query method like this ....
SELECT
[col1],
[col2],
SELECT(if exists (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'Table' AND COLUMN_NAME = 'col3') BEGIN SELECT [col3]...
Milan source
share