I am trying to add a column to a table, and then update that newly added column. these two things are relatively simple, however when I place them inside an 'if' statement, i get the error:
Invalid column name 'TOTAL_VOTES'
Here is my SQL:
if (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'RESULTS'
AND COLUMN_NAME = 'TOTAL_VOTES'
) = 0
ALTER TABLE [dbo].[RESULTS] ADD [TOTAL_VOTES] int not NULL DEFAULT (0); --WITH VALUES
UPDATE RESULTS SET TOTAL_VOTES = (OPTION1 + OPTION2 + OPTION3 + OPTION4 + OPTION5 + OPTION6 + OPTION7 + OPTION8 + OPTION9 + OPTION10)
i need the if check b/c I only want to add and populate the column if it doesn't already exist in the table. (prior to this I check to see if the table exists)
I have tried to set a variable and then based off of that variable, populate the new column, but that doesn't work either.
any suggestions? Why does the if() begin/end seem to cause errors with the alter/update statements?
How can I get around this?