游标循环插入临时表

微醺_zZ 2009-08-14 10:52:07

declare @Code uniqueidentifier
declare list cursor for select distinct Code from A
open list
fetch next from list into @Code
while (@@ROWCOUNT>0)
begin
Select * into #tbl From A
fetch next from list into @Code
end
close list
DEALLOCATE list


游标执行一遍之后出现错误提示:数据库中已存在名为 '#tbl' 的对象。

请问,我想将每次游标循环查出来的数据都插入到这个临时表中该怎么做?

...全文
79 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
水族杰纶 2009-08-14
  • 打赏
  • 举报
回复
create table  #tbl()
declare @Code uniqueidentifier
declare list cursor for select distinct Code from A
open list
fetch next from list into @Code
while (@@ROWCOUNT>0)
begin
insert #tbl Select * From A
fetch next from list into @Code
end
close list
DEALLOCATE list
  • 打赏
  • 举报
回复
create table @tb1( ……)
declare @Code uniqueidentifier

declare list cursor for select distinct Code from A
open list
fetch next from list into @Code
while @@fetch_status=0
begin
Select * From A
fetch next from list into @Code
end
close list
DEALLOCATE list

27,580

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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