簡體   English   中英

將選擇查詢中的值存儲到變量中

[英]store values from select query to variables

我想將選擇查詢中的值存儲到聲明的變量中,但是這里的問題是我的選擇查詢返回了多行。 見下面的例子

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

關於如何實現這一目標的任何想法都會有很大幫助。 如果您需要更多說明,請告訴我。

預先感謝,甘根

我認為您正在尋找游標

是一個很好的鏈接。

我會說您應該使用表變量(或臨時表)來存儲多個值。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM