借用supsuccess(火气不小) 的办法, but the runtime behavior is unpredictablecreate proc getFixedSumedRecord
@nCountParam int,
@nSumParam int
as
begin
set nocount on
declare @count int
declare @sum int
declare @sql varchar(1000)set @count = 0
set @sum = 0select top 1 * into #mytable from aset @sql = 'insert into #mytable select * from ( select top ' + convert(varchar,@nCountParam) + ' * from a order by NewID()) as t'while (@count <>@nCountParam) Or (@sum <> @nSumParam) --sometimes not exact @nCountParam records were returned
begin
delete from #mytable
exec (@sql)
select @count = count(*), @sum = sum(num) from #mytable
end
set nocount off
select * from #mytable
endgetFixedSumedRecord N,m