概念:
排列: P(n,r) 从n个中选r个进行排列(与顺序有关)
组合: C(n,r) 从n个中选r个进行组合(与顺序无关)
create procedure sp_test(@n int,@r int)
as
begin
if isnull(@n,0)<isnull(@r,0)
return
set rowcount @n
select identity(int,1,1) as num into # from sysobjects a,syscolumns b
set rowcount 0
declare @sql varchar(8000),@ord varchar(8000),@i int
set @sql='select * from # [1]'
set @ord='[1].num'
set @i=1
while @i<@r
begin
set @i=@i+1
set @sql=@sql+' inner join # ['+rtrim(@i)+'] on ['+rtrim(@i)+'].num>['+rtrim(@i-1)+'].num'
set @ord=@ord+',['+rtrim(@i)+'].num'
end
set @sql=@sql+' order by '+@ord
print @sql
exec(@sql)
end
go
exec sp_test 5,1
exec sp_test 5,2
exec sp_test 5,3
go
drop procedure sp_test
go
排列: P(n,r) 从n个中选r个进行排列(与顺序有关)
组合: C(n,r) 从n个中选r个进行组合(与顺序无关)
create procedure sp_test(@n int,@r int)
as
begin
if isnull(@n,0)<isnull(@r,0)
return
set rowcount @n
select identity(int,1,1) as num into # from sysobjects a,syscolumns b
set rowcount 0
declare @sql varchar(8000),@ord varchar(8000),@i int
set @sql='select * from # [1]'
set @ord='[1].num'
set @i=1
while @i<@r
begin
set @i=@i+1
set @sql=@sql+' inner join # ['+rtrim(@i)+'] on ['+rtrim(@i)+'].num>['+rtrim(@i-1)+'].num'
set @ord=@ord+',['+rtrim(@i)+'].num'
end
set @sql=@sql+' order by '+@ord
print @sql
exec(@sql)
end
go
exec sp_test 5,1
exec sp_test 5,2
exec sp_test 5,3
go
drop procedure sp_test
go