views:

55

answers:

3

Hi, I have a stored procedure in T-Sql with this code at the end of it:

UPDATE @Results
SET Percentage = CASE B.Total
WHEN 0 THEN 0
ELSE (CAST(Number AS FLOAT)/B.Total)
END
FROM @TotalAnswersPerQuestion AS B
WHERE @Results.QuestionNumber=B.QuestionNumber

The temp table @Results is defined and correctly used in this store procedure just before these instructions, but I get an error on the last line saying:

Msg 137, Level 15, State 2, Procedure Report, Line 111 Must declare the scalar variable "@Results".

This are my tables:

DECLARE @TotalAnswersPerQuestion Table
(QuestionNumber int,
Total int)

DECLARE @Results Table
(
    QuestionNumber int,
    QuestionTitle varchar(max),
    AnswerNumber int,
    AnswerLable varchar(max),
    ProfileGroupID int,
    [Name] varchar(255),
    Identifier varchar(20),
    Number int,
    Percentage float
)

What's wrong?

+4  A: 
UPDATE r
SET Percentage = 
    CASE B.Total
        WHEN 0 THEN 0
        ELSE (CAST(Number AS FLOAT)/B.Total)
    END
FROM @TotalAnswersPerQuestion AS B
INNER JOIN @Results r
ON r.QuestionNumber=B.QuestionNumber;

I am shameless about UPDATE FROM! Let the flaming begin!

Dave Markle
+1  A: 

Alias the table:

UPDATE r
SET Percentage = CASE B.Total
WHEN 0 THEN 0
ELSE (CAST(Number AS FLOAT)/B.Total)
END
FROM   @Results r
JOIN   @TotalAnswersPerQuestion AS B
ON     r.QuestionNumber=B.QuestionNumber
Quassnoi
A: 
UPDATE r
SET Percentage = CASE B.Total
WHEN 0 THEN 0
ELSE (CAST(Number AS FLOAT)/B.Total)
END
FROM @Results r
,@TotalAnswersPerQuestion AS B
WHERE r.QuestionNumber=B.QuestionNumber
Kamal