改用臨時表try
declare @sql nvarchar(4000)
       Create table #table (newid int identity(1,1),tid int) 
       set @sql = 'insert into  #table(tid) select ' + @PK  + ' from ' + @Tables + @strFilter + ' ' + @strGroup + ' ORDER BY ' + @Sort  
       +' select ' + @Fields + ' from ' + @Tables + ' a join #table b on a.' +@PK + '=b.tid and  b.newid between ' 
+ convert(varchar(20),@begin_id) + ' and '
                           + convert(varchar(20),@end_id) + '  '  + @strSimpleFilter + ' ' + @strGroup + ' ORDER BY ' + @Sort 
      
       exec sp_executesql @sql