2013-05-07 68 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