Blog Pages

Error when create new column and run some things on it

if not exists (select * from sys.columns where object_id = object_id('dbo.MyTable') and name = 'NewColumn')
begin
                alter table Customer.Configuration add NewColumn int null
end
do something with the new column - update, alter, etc.
--> Error message:
Msg 207, Level 16, State 1, Server <MyServer>, Line 17 Invalid column name 'NewColumn'.

Solution:
Close the batch pf the creation by adding 'GO' between the column creatuion and usage.

 if not exists (select * from sys.columns where object_id = object_id('dbo.MyTable') and name = 'NewColumn')
begin
                alter table Customer.Configuration add NewColumn int null
end
GO
do something with the new column - update, alter, etc.

No comments:

Post a Comment