views:

21

answers:

1

I can not for the life of me figure out what is wrong with this code:

IF NOT EXISTS(SELECT * FROM sys.columns WHERE name = 'Report_Date' 
                       AND object_id = OBJECT_ID('TempTable3'))
    ALTER TABLE TempTable3 ADD Report_Date datetime

--Set "ReportDate" field to the date the data was extracted less one.
UPDATE TempTable3
SET Report_Date = '20100815'

I keep getting this error:

"Msg 207, Level 16, State 1, Line 51
Invalid column name 'Report_Date'."
+1  A: 

Put a GO after alter table

ALTER TABLE TempTable3 ADD Report_Date datetime
GO
SQLMenace
It seems to have worked, but why was it necessary?
because GO is a batch terminator telling SQL Server to execute immediately up to that point. It is not needed for updates and inserts but whenever you change meta data it needs it
SQLMenace