用TOP代替一般游标

chenxin2835 2008-06-03 06:10:51
没事发个帖,代表我还存活着。
以前弄的用TOP取值来代替一般游标取值的法子,效率如何,没经过测试......有个前提,得有个排序用的列,如ID
Declare @o Bigint,
@n Varchar(256),
@h Int
Set @o=0
Set @n=''
Set @h=0
Select @h=Count(*) From table1
While @h>0
Begin
Set Rowcount @h

Select ID,A,B
Into #temp
From table1
order by ID desc

Select Top 1 @o=A,@n=B
From #temp
order by ID

Set @h=@h-1
Drop Table #temp
Set rowcount 0
End
...全文
85 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
chenxin2835 2008-06-04
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 DVD_01 的回复:]
有问题,

While ...
Begin
-- 频繁的Into #temp 和Drop Table #temp ,不如把Into #temp部分拿到While外面

End

e.g:

use test
go

Declare @o int,
@n Varchar(256),
@h datetime

If object_id('#') Is Not null
Drop Table #
Select Rid=Identity(int,1,1),Name,WorkDate Into # From DutyHistory
Set @o=@@Rowcount
While @o>0
Begin
Select @n=Name,@h=WorkD…
[/Quote]
学习了,谢谢
Andy-W 2008-06-03
  • 打赏
  • 举报
回复
有问题,

While ...
Begin
-- 频繁的Into #temp 和Drop Table #temp ,不如把Into #temp部分拿到While外面

End

e.g:

use test
go

Declare @o int,
@n Varchar(256),
@h datetime

If object_id('#') Is Not null
Drop Table #
Select Rid=Identity(int,1,1),Name,WorkDate Into # From DutyHistory
Set @o=@@Rowcount
While @o>0
Begin
Select @n=Name,@h=WorkDate From # Where Rid=@o
Select @n,@h --执行语句过程
Set @o=@o-1
End
hery2002 2008-06-03
  • 打赏
  • 举报
回复
o o
shuhua 2008-06-03
  • 打赏
  • 举报
回复
..
中国风 2008-06-03
  • 打赏
  • 举报
回复
..
liangCK 2008-06-03
  • 打赏
  • 举报
回复
jf

34,838

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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