Go down
Posts : 155
Join date : 2017-11-11
Age : 27
View user profile

SQL delta script shows error when using a Column which will be deleted

on Wed Dec 13, 2017 12:04 pm
I've got a sql delta script to update my database for a running application. In this delta script I have a SELECT statement on a column which will be deleted afterwards.

The statement is surrounded by an if clause and won't be executed, if the column does not exist. At the first run there is no problem, but at any further runs of the delta script an error is shown "Invalid column name 'column name'".

How can I prevent this error?

You are getting this error because at compilation time SQL does an object existence check for the script. You can avoid this by converting your query to a query string. like this
IF EXISTS(SELECT 1 FROM sys.columns WHERE object_id = object_id('YourTable') and name = 'ColumnName')

exec('select ColumnName from YourTable where <Conditions>')

Back to top
Permissions in this forum:
You can reply to topics in this forum