Is there any way to select a column if it exists in the view, but ignore the column if it does not exist?
SELECT
CASE
WHEN EXISTS(SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'MyView' AND COLUMN_NAME = 'MyColumn')
THEN MyView.MyColumn
ELSE NULL
END AS [Sometimes]
FROM
MyView
This currently returns the error "Msg 207 Invalid column name".
Perhaps there is an opportunity to ignore this error?
source
share