使用ROW_NUMBER()+临时表+While 实现表遍历


declare @table table
(
dlid int,
RowNum int
)
insert into @table select dlid,ROW_NUMBER() over(order by dlid) as RowNum  from dbo.tblPublicationGroupMapping
where dlid in
(
select dlid from dbo.tblDLMaster where DLDetailsXML.value('(/dpsi/@dpsiCode)[1]','VARCHAR(50)') like '%T-%')
and GroupID in (
select GroupID from dbo.tblPublicationGroup where GroupType=1
)
declare @row int=1
while EXISTS(select * from @table)
begin

declare @dlid int
select @dlid=dlid from @table where RowNum=@row

print @dlid

delete from @table where RowNum=@row

 set @row=@row+1;
end

原文地址:https://www.cnblogs.com/LittleFeiHu/p/6202783.html