不知道有沒理解錯誤﹐ 可以試試看這樣可不可以達到你要的效果
Create Table #Table (sized int,dir varchar(1))
Insert into #Table Values(123,'a')
Insert into #Table Values(321,'b')
Insert into #Table Values(321,'d')
Insert into #Table Values(456,'e')
Insert into #Table Values(576,'f')
Insert into #Table Values(852,'g')
Insert into #Table Values(852,'h')
Insert into #Table Values(798,'i')
Insert into #Table Values(798,'j')
Insert into #Table Values(798,'k')
Insert into #Table Values(500,'l')
Insert into #Table Values(1542,'m')
Insert into #Table Values(1542,'n')
Insert into #Table Values(578,'o')
Insert into #Table Values(1234,'p')
Insert into #Table Values(5387,'q')Select  IDentity (int,1,1) As ID,*
Into #TempTable
From #Table 
Order By Sized 
Select * From #TempTable Declare @10000 int,@int int,@Dir varchar(300),@CDir varchar(1),@Max int
Select @Max=Count(*),@10000=0,@Dir='(' From #TempTable
While @Max>=1
Begin
   Select @int=Sized,@CDir=Dir From #TempTable Where ID=@Max
   Select @10000=@10000+@int
   Select @Dir=@Dir+''''+@CDir+''''+','
   IF @10000>10000 
   Break
   Select @Max=@Max-1
   Select @10000,@int
End
   Select @Dir=Substring(@Dir,1,Len(@Dir)-1)+')'
Print(' Select * From #Table Where Dir in '+@Dir)
Exec(' Select * From #Table Where Dir in '+@Dir)