2

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?

0

3 Answers 3

4
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!

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.