不用游標也能遍歷記錄的sql語句實例
在SQL數據庫中,不使用游標,也能遍歷記錄?答案是肯定的,下面就為您介紹不用游標也能遍歷記錄的sql語句實例,供您參考。
--聲明變量表@tb
declare @tb table(id int,name varchar(50))
--添加測試數據
insert into @tb
select 6,'aa' union all
select 7,'bb' union all
select 8,'cc' union all
select 9,'dd' union all
select 10,'abc' union all
select 11,'ddef' union all
select 12,'fda' union all
select 13,'rewr' union all
select 14,'eyt' union all
select 15,'jjy' union all
select 16,'bbbxd' union all
select 17,'xxx' union all
select 18,'ffff' union all
select 19,'wwwwwwww' union all
select 20,'aaaaaaaaaa'
/*
查看表中數據
select * from @tb
*/
--聲明循環用的“指針”
declare @min varchar(5)
--賦初值
select @min=min(id) from @tb
--開始循環
while @min is not null
begin
print @min --打印當前“指針”的值
select @min=min(id) from @tb where id>@min --更新“指針”內容,使之移到下一記錄
end
【編輯推薦】