当前位置: 代码迷 >> Sql Server >> 怎么把游标查询出来的结果合并成一个结果集呀?求教(小弟我是刚注册的)
  详细解决方案

怎么把游标查询出来的结果合并成一个结果集呀?求教(小弟我是刚注册的)

热度:42   发布时间:2016-04-27 18:55:25.0
如何把游标查询出来的结果合并成一个结果集呀??求教(我是刚注册的)

alter   proc   tieview
[email protected]   int
as
declare   mycursor   cursor   for
select   author,id   from   tie   where   biaoji=2


declare   @aa   varchar(50)
declare   @bb   int
open   mycursor
fetch   next   from   mycursor   into   @aa,@bb

while   @@fetch_status=0
begin
select   a.*,b.content   from   usermsg   a,tie   b   where   [email protected]   and   [email protected]
fetch   next   from   mycursor   into   @aa,@bb
end

close   mycursor

deallocate   mycursor

这个得出好几个结果
我想把他们合并成一个结果集,我用UNION   ALL,但写到游标循环里就出错.实在没法了..求教各位了

------解决方案--------------------
作过表变量,插进去就行了
------解决方案--------------------
也可以建全局临时表或建函数返回表集合(而非proc)
------解决方案--------------------
对应你的select a.*,b.content from usermsg a,tie b where [email protected] and [email protected]
定义一个临时表.
游标每执行一次.插入一条记录
  相关解决方案