SQL 将选择查询的结果存储到数组变量中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36009422/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 04:29:19  来源:igfitidea点击:

store result of select query into array variable

sqlsql-serversql-server-2012

提问by Mohamad

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 查询的结果存储在变量 a 中,结果将由 17 行组成 如何编辑此代码以将其存储在 @a 中

declare @a uniqueidentifier
select EnrollmentID into @a  from Enrollment

回答by Giorgos Betsos

You cannot store 17 values inside a scalarvariable. You can use a tablevariable instead.

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

This is how you can declare it:

这是您可以声明它的方式:

DECLARE @a TABLE (id uniqueidentifier)

and how you can populate it with values from Enrollmenttable:

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

INSERT INTO @a 
SELECT EnrollmentID FROM Enrollment

回答by S.Karras

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;