SQL Server 之 使用RowCount遍历表数据

create table nametable(
nameguid uniqueidentifier,
name nvarchar(10)
)
insert into nametable(nameguid,name) values(newid(),'lide')
insert into nametable(nameguid,name) values(newid(),'lideqiang')
insert into nametable(nameguid,name) values(newid(),'liqiang')

set rowcount 1
select * from nametable

set rowcount的作用是使 SQL Server在返回指定的行数之后停止处理查询。

输出结果:

  

实现表遍历:

declare @nameguid uniqueidentifier

declare @name nvarchar(10)

while exists (select * from nametable)

begin

set rowcount 1

select @nameguid = nameguid from nametable

select @name = name from nametable where nameguid = @nameguid

set rowcount 0

delete from nametable where nameguid = @nameguid

print @name

end

不过这样操作会删除表中的数据,可以在临时表中操作

select * into #nametable from nametable

 

posted @ 2015-05-03 18:01  DeqiangLee  阅读(351)  评论(0编辑  收藏  举报