如何在有int型主键遍历表中的某一列数据

 

create table test1(ID int,UserName nvarchar(10))
select * from test1
insert test1 values (1,'11')
insert test1 values (2,'22')
insert test1 values (3,'33')
insert test1 values (4,'44')
insert test1 values (5,'55')

declare @id int
declare @username varchar(20)
select @id=min(ID) from test1
while @id is not null
begin
 select @username= UserName from test1 where ID=@id
print @username 
 select @id=min(ID) from test1 where ID>@id
end

posted @ 2011-07-23 09:27  haiziguo  阅读(208)  评论(0编辑  收藏  举报