日期:2014-05-18  浏览次数:20570 次

在存储过程中怎么取得临时表中的每一行的数据
比如select   top   3   id   into   #aa   from   b
declare   @id1   int,
@id2   int,
@id3   int

怎么把#aa中的三条数据select出来分别给@id1,@id2,@id3

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

循环每行,给三个变量赋值。
------解决方案--------------------
可不用游标,多声明一个变量@i
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),@i=@i+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
------解决方案--------------------
最好别用游标