Share
Go down
avatar
Admin
Posts : 155
Join date : 2017-11-11
Age : 27
View user profilehttp://net-media.yoo7.com

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?

Answer
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
Code:
IF EXISTS(SELECT 1 FROM sys.columns WHERE object_id = object_id('YourTable') and name = 'ColumnName')
begin

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

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