将select查询的结果存储到数组变量中

时间:2020-11-25 18:30:38

I want to store the result of this sql query in variable a the result will be formed of 17 row how to edit this code in order to store it in @a

我想将这个sql查询的结果存储在变量中,结果将由17行组成,如何编辑这段代码以便将它存储在@a中

declare @a uniqueidentifier
select EnrollmentID into @a  from Enrollment

2 个解决方案

#1


8  

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

您不能在标量变量中存储17个值。您可以使用表变量。

This is how you can declare it:

这是你如何声明它:

DECLARE @a TABLE (id uniqueidentifier)

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

以及如何使用Enrollment表中的值填充它:

INSERT INTO @a 
SELECT EnrollmentID FROM Enrollment

#2


2  

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

您应该将@a声明为具有一列唯一标识符类的表变量,如下所示:

DECLARE @a TABLE (uniqueId uniqueidentifier); 

INSERT INTO @a
SELECT EnrollmentID 
FROM Enrollment;

#1


8  

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

您不能在标量变量中存储17个值。您可以使用表变量。

This is how you can declare it:

这是你如何声明它:

DECLARE @a TABLE (id uniqueidentifier)

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

以及如何使用Enrollment表中的值填充它:

INSERT INTO @a 
SELECT EnrollmentID FROM Enrollment

#2


2  

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

您应该将@a声明为具有一列唯一标识符类的表变量,如下所示:

DECLARE @a TABLE (uniqueId uniqueidentifier); 

INSERT INTO @a
SELECT EnrollmentID 
FROM Enrollment;