14

I want to store the result of this sql query in variable @a. The result contains 17 rows. How to edit this code in order to store the rows in @a?

declare @a uniqueidentifier
select EnrollmentID into @a  from Enrollment

2 Answers 2

21

You cannot store 17 values inside a scalar variable. You can use a table variable instead.

This is how you can declare it:

DECLARE @a TABLE (id uniqueidentifier)

and how you can populate it with values from Enrollment table:

INSERT INTO @a 
SELECT EnrollmentID FROM Enrollment
Sign up to request clarification or add additional context in comments.

Comments

4

You should declare @a as a Table Variable with one column of type unique identifier as follows:

DECLARE @a TABLE (uniqueId uniqueidentifier); 

INSERT INTO @a
SELECT EnrollmentID 
FROM Enrollment;

1 Comment

how your answer is different from the accepted answer ?

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.