I tried to do this but couldn't find a way
I have a t-sql script that adds a new column to a table, then populates these columns with values depending on some other columns in the same table and finally deletes some columns. All of this works great.
The problem occurs when I want to run the script again. I have an if clause that checks for missing columns, but SSMS still complains and displays an error message even if the code inside the if clause is not running. The script should be able to run more than once, and I don’t want the error messages to be displayed!
In code (obviously, test code, no need to unload production code here ...):
create table test ( Name text, Switch int, ValueA int, ValueB int) go insert into test values ('Name', 0, 5, 10) if not exists (select 1 from INFORMATION_SCHEMA.COLUMNS where COLUMN_NAME = 'ValueC' and TABLE_NAME = 'test') begin alter table test add ValueC int end go
Here is the error message:
Msg 207, Level 16, State 1, Line 6 Invalid column name 'ValueA'. Msg 207, Level 16, State 1, Line 7 Invalid column name 'ValueA'.
Greetings --Jocke
Jocke source share