当前位置: 代码迷 >> Sql Server >> 在存储过程中如何取得临时表中的每一行的数据
  详细解决方案

在存储过程中如何取得临时表中的每一行的数据

热度:99   发布时间:2016-04-27 16:15:07.0
在存储过程中怎么取得临时表中的每一行的数据
比如select   top   3   id   into   #aa   from   b
declare   @id1   int,
@id2   int,
@id3   int

[email protected],@id2,@id3

------解决方案--------------------
游标
------解决方案--------------------
用游标。

循环每行,给三个变量赋值。
------解决方案--------------------
可不用游标,[email protected]
declare @id1 int,@id2 int,@id3 int,@i int
select @id1=0,@id2=0,@id3=0,@i=1
update #aa set @id1=(case when @i=1 then id else @id1 end),
@id2=(case when @i=2 then id else @id2 end),
@id3=(case when @i=3 then id else @id3 end),@[email protected]+1
select @id1,@id2,@id3
------解决方案--------------------
declare @id1 int,
@id2 int,
@id3 int

declare myCur cursor
for
select top 3 id from b
open myCur
fetch next from myCur into @id1
fetch next from myCur into @id2
fetch next from myCur into @id3
select @id1 as 'id1 ',@id2 as 'id2 ',@id3 as 'id3 '
close myCur
deallocate myCur
------解决方案--------------------
最好别用游标
  相关解决方案