单个组中的SQL游标值

时间:2010-01-24 03:49:06

标签: sql-server tsql cursor

我正在使用SQL游标。我想选择值

While(@@fetchstatus ==0)
BEGIN
  if(cond)
    SELECT rownumber, rowname FROM TABLE
END

在第一轮执行While循环期间,我得到了值。

1,“Firstrow”(来自行选择)

下一步

2, “SecondRow”

所有这些值在我的输出中显示为单独的行。是否可以组合并显示为这样的两个输出

  

1,“FirstRow”

     

2, “SecondRow”

1 个答案:

答案 0 :(得分:0)

如果你真的需要光标上的循环,这可能会有所帮助:

-- create temptable
select top 0 rownumber, rowname 
into #temp
from table

-- loop through your cursor
While(@@fetchstatus ==0)
BEGIN
  if(cond)
  begin
    insert into #temp
    SELECT rownumber, rowname FROM TABLE
  end
END

select rownumber, rowname from #temp

drop #temp