使用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

posted @ 2016-12-20 15:16  麻将我会  阅读(291)  评论(0编辑  收藏  举报