How to update temp table from another table

Viewed 5499

I have [DETAIL] table with columns DetailID, PackageID, TemplateID and DocumentID. The DetailID column is the primary key.

Then in the stored procedure, I use a temp table

DECLARE @temp TABLE
(
    DetailID INT NULL,
    PackageID INT,
    TemplateID INT NULL,
    DocumentID INT NULL,        
)

The temp table is already populated with PackageID, TemplateID and DocumentID. I am trying to update Temp table's DetailID with matching rows. For non matching rows DetailID will be null in temp table

UPDATE @temp 
SET DetailID = D.DetailID
FROM Details D
WHERE D.PackageID = @temp.PackageID AND 
                  ((D.TemplateID IS NOT NULL AND D.TemplateID = @temp.TemplateID) OR
                  (D.DocumentID IS NOT NULL AND D.DocumentID = @temp.DocumentID))

I am getting an error

Must declare the scalar variable "@temp"

2 Answers
Related