不用游標(biāo)也能遍歷記錄的sql語(yǔ)句實(shí)例
在SQL數(shù)據(jù)庫(kù)中,不使用游標(biāo),也能遍歷記錄?答案是肯定的,下面就為您介紹不用游標(biāo)也能遍歷記錄的sql語(yǔ)句實(shí)例,供您參考。
--聲明變量表@tb
declare @tb table(id int,name varchar(50))
--添加測(cè)試數(shù)據(jù)
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'
/*
查看表中數(shù)據(jù)
select * from @tb
*/
--聲明循環(huán)用的“指針”
declare @min varchar(5)
--賦初值
select @min=min(id) from @tb
--開(kāi)始循環(huán)
while @min is not null
begin
print @min --打印當(dāng)前“指針”的值
select @min=min(id) from @tb where id>@min --更新“指針”內(nèi)容,使之移到下一記錄
end
【編輯推薦】