I have a little exercise where I need to access data from a view and print it out in a report. I have created a #temporary table to store the data in and the to retrieve it and display it in the report by using a while loop.
Problem is the temporary table seems to go ‘missing’.
--Creating my report
USE PetShopDataBase
CREATE PROCEDURE spPetShopReport
@customerID INT
SELECT *
INTO #temporary
FROM vwPetshop
WHERE customerID = @customerID
GO
ALTER TABLE #temporary
ADD Printed SMALLINT
GO
Then from this point the object is regarded as invalid
UPDATE #temporary
SET Printed = 0
GO
the error message I get when I run the code is
Msg 4902, Level 16, State 1, Line 2
Cannot find the object "#temporary" because it does not exist or you do not have
permissions.
Why is that?
Kind regdards
Do not use GO inside of a stored proc. Go ends the batch and thus the stored proc.
BTW way all of this code can be compressed into one statement
try this instead: