I am attempting to update a field that was just added from an ALter
statement. I am receiving an error of:
Msg 207, Level 16, State 1, Procedure TestProc, Line 436
Invalid column name 'discountamount'.
This is my syntax that I am attempting to use – what would be the proper way to update a field just added in a stored procedure?
ALTER TABLE [dbo].msnbc
ADD saleamount float,
discountamount float;
UPDATE ftri
SET ftri.discountamount = fji.Totaldiscountamount
FROM [dbo].msnbc ftri
INNER JOIN [dbo].halfbaked fji
ON ftri.saleid = fji.saleid
Best Answer
If you need to do this in a stored procedure, your update has to be dynamic.
Here's a demo in case you get lost on the way.
Create a simple table:
This is about what your stored procedure will have to look like:
There won't be any errors because the dynamic SQL isn't compiled at this stage.
Then you can run it:
And validate results:
Hope this helps!