I have the following table (let’s say that its name is “AnswerTable”:
QuestionID Value
100 1
101 2
102 1
103 2
104 1
105 2
...
Then I have several variables:
DECLARE @Answer1 INT
DECLARE @Answer2 INT
....
and I need to retrieve information from the table like this:
SET @Answer1 =(SELECT Value FROM AnswerTable WHERE QuestionID=100)
SET @Answer2 =(SELECT Value FROM AnswerTable WHERE QuestionID=101)
...
And what I want to do is to initialize the variables with only one select from the table, because in my real situation the table is very big and it is joined with other tables and I am concerned that multiple execution of the sub query is not going to be efficient.
Is there an easy way to do this?
In General;
In Your case;
Ex: Sql Fiddle Example