2013-05-07 69 views
1

我想将值存储到来自select查询的声明变量,但这里的问题是我的select查询返回多行。见下面从选择查询到变量的商店值

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... 

例如在如何实现这一目标将有很大的帮助,任何想法。 如果您需要更多解释,请让我知道。

由于提前, 加甘

+1

其他人提出了各种想法(表变量,临时表),但不清楚你正在尝试做什么或将如何使用变量。如果你能展示你想如何使用它们,有人可能会提出一个好的解决方案。 – Pondlife 2013-05-07 15:58:08

回答

1

我认为你正在寻找cursors

Here是一个不错的链接探索它。

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