2012-06-24 37 views
0

什麼是SQL Server 2008中選擇一些可以包含數據的10名最好的方法,然後將數據進行比較,在它的一個特定值的列SQL選擇多行數據再對比

因此,像下面這樣

SELECT bType FROM WORK_STATION WHERE nFileId = 123456789 

這可能會返回要麼1 - 10個值MAX(將至少返回一個值)。然後將數據從上面,我們只是選擇了一個特定的值,以類似

if bType = 1 
--DO something 

什麼是做這樣的事情最好的辦法,SQL語句比較?

回答

0

我認爲你可以使用SP來聲明變量,然後將它與結果集進行比較,如果你知道只有10個值,你可以使用臨時表並插入10個值。

我希望這是有幫助的。

1
declare @table as table(btype int) 
declare @btype int 

insert into @table 
SELECT bType FROM WORK_STATION WHERE nFileId = 123456789 

while(exists(select top 1 'x' from @table)) --as long as @table contains records continue 
begin 
    select top 1 @btype = btype from @table 

    if(@btype = 10) 
    print 'something' 

    delete top (1) from @table --remove the previously processed row. also ensures no infinite loop 
end