0

What would be the best approach in SQL Server 2008 to select something that can contain 10 list of data, then compare that data with a specific value in one of it's columns

So something like this below

SELECT bType FROM WORK_STATION WHERE nFileId = 123456789

Which could return either 1 - 10 values MAX (will return at least one value). Then to compare the data from that SQL statement above that we just selected to a specific value to something like

if bType = 1
--DO something

What is the best approach of doing something like this?

4

2 回答 2

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 
于 2012-06-24T07:20:48.977 回答
0

我认为您可以使用 SP 声明变量,然后将其与结果集进行比较,如果您知道您只有 10 个值,您可以使用临时表并插入 10 个值。

我希望这是有帮助的。

于 2012-06-24T06:59:00.570 回答