Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

store result of select query into array variable

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
like image 253
Ahmad Avatar asked Mar 15 '16 11:03

Ahmad


2 Answers

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
like image 154
Giorgos Betsos Avatar answered Oct 31 '22 00:10

Giorgos Betsos


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;
like image 3
S.Karras Avatar answered Oct 31 '22 01:10

S.Karras