1

我想将值存储到选择查询中声明的变量中,但这里的问题是我的选择查询返回多行。请参阅下面的示例

select Col1
from Table1 where Col1 NOT IN (select Col1 from Table2) 
and Col3 >=8


------------------
result
73
74

declare @temp1 int
declare @temp2 int

I essentially want @temp1 to hold 73 and @temp2 hold 74 and so fort...

任何关于如何实现这一目标的想法都会有很大帮助。如果您需要更多解释,请告诉我。

提前致谢, 加根

4

2 回答 2

1

我会说你应该使用表变量(或者可能是临时表)来存储多个值。

declare @tab table
    (
        col1 int
    );

with myTab as
(
    Select 1 col
    Union All 
    Select 2
    Union All
    Select 3
)
Insert Into @tab 
    Select Col 
    From MyTab

Select * From @tab
于 2013-05-07T11:25:33.793 回答
1

我认为您正在寻找cursors

是一个很好的链接解释它。

于 2013-05-07T11:22:39.437 回答