存储查询结果,将多个结果作为列表变量以备后用

pla*_*ums 5 sql sql-server

我想将 select 语句的结果放入 @variable 中,以便稍后在查询中使用。我不知道会有多少结果。我试过了;

SET @variable = SELECT column FROM table
Run Code Online (Sandbox Code Playgroud)

结果

@variable=( 123213,321312,321321)
Run Code Online (Sandbox Code Playgroud)

然后我想将结果用作

UPDATE table SET column=1 WHERE column in @variable
Run Code Online (Sandbox Code Playgroud)

Gor*_*off 5

只需使用临时表:

SELECT column
INTO #tmp
FROM table;


UPDATE table
    SET column = 1
    WHERE column in (SELECT column FROM #tmp);
Run Code Online (Sandbox Code Playgroud)

您也可以使用表变量,但这需要指定列的类型来定义变量。