sql server - Is it possible to tell SSMS not to check if a column exists in a t-sql script? -


I tried to google it, but I could not find any way

SQL script that adds a new column to a table, then fills the column based on a few other columns in the same table and finally removes some columns. All this works well.

The problem occurs when I want to run the script again. I have a section that checks that the missing columns are present, but the SSMS is still complaining and sends an error message, even if the code does not run inside the cloud. The script should be able to run once more, and I do not want to display error messages!

In the code (clearly the test code, do not want to dump the output code here ...):

  Test the table (name text, rename, Switch At, Value at Value Value, Value Value, Value Value, Value, Value, Value, Value, and TABLE_NAME = 'test', 'Value' and 'value' and 'value' in the test values ​​('name', 0, 5, 10) if it is not present (1 select INFORMATION_SCHEMA.COLUMNS ') Begin changing the table test ValueC int end go - This batch rasies error when run again! If present (select 1 from INFORMATION_SCHEMA.COLUMNS where COLUMN_NAME = 'ValueA' and TABLE_NAME = 'test') begin setting up the test ValueC = (Select the case then select 0 (select (Value A - ValueB )) 1 Then select (ValueB - Value)) End (if the interval exists, select 1 from INFORMATION_SCHEMA.COLUMNS where COLUMN_NAME = 'ValueA' and TABLE_NAME = 'test') table test drop column begins to change There is an error message here:  
  Message 207, the value is from the Values ​​End and Test * Select - name 0 10 -5  

16, State 1, Line 6 Invalid column name 'Value A' Message 207, Level 16, State 1, Line 7 Invalid column name 'Value A'.

Cheers - Joke

Yes it is possible without dynamic SQL but with a bit of a cli comprehension workaround.

behavior in SQL 2000

Erland Sommarskog mentions "Once all queries exist in a query, SQL Server completes Investigation on Eri. "

Thus, adding an option to the query in the query is not present in the table that does not exist, the compilation can be postponed. With this adjustment, run the script below several times without getting the error Enter the test values ​​('name', 0, 5, 10) if it is not present (select 1 from INFORMATION_SCHEMA COLUMN_NAME = 'ValueC' and 'TABLE_NAME =' test. ') Start changing the table test ValueC int end go create table #dummy (i int) - this batch is once again An error occurred while running! If present (select 1 from INFORMATION_SCHEMA.COLUMNS where COLUMN_NAME = 'ValueA' and TABLE_NAME = 'test') begin setting up the test ValueC = (Select Case Select when 0 (Selection (Value A - ValueB) 1 Then select (ValueB - Value A) end) where not present (choose # from dummy *) End drop table #dummy go if present (from INFORMATION_SCHEMA.COLUMNS to 1 Select where COLUMN_NAME = 'ValueA' and TABLE_NAME = 'test') begin to change the table test drop column Value End Test * Test from - Name 0 10 -5


Comments