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

妖精的舌头 2007-05-22 11:10:06

alter proc tieview
--@id 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 a.username=@aa and b.id=@bb
fetch next from mycursor into @aa,@bb
end

close mycursor

deallocate mycursor

这个得出好几个结果
我想把他们合并成一个结果集,我用UNION ALL,但写到游标循环里就出错.实在没法了..求教各位了
...全文
485 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
ankor 2007-05-22
  • 打赏
  • 举报
回复
也可以建全局临时表或建函数返回表集合(而非proc)
chuifengde 2007-05-22
  • 打赏
  • 举报
回复
作过表变量,插进去就行了
妖精的舌头 2007-05-22
  • 打赏
  • 举报
回复
谢谢各位
我也想到建个临时表了..哈
问题就应该解决了
还有...怎么把20分给你们?不知道咋弄,是要去斑竹那的吗?
可否告知...
分治实践 2007-05-22
  • 打赏
  • 举报
回复
对应你的select a.*,b.content from usermsg a,tie b where a.username=@aa and b.id=@bb
定义一个临时表.
游标每执行一次.插入一条记录

22,207

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧