------------------------MSSQL中排列组合的实现------------------------------------
来源:互联网 发布:2016chinajoy数据 编辑:程序博客网 时间:2024/05/19 03:16
--排列组合
--执行方法:exec [Arrange] '1,2,3'
--input:1,2,3
--output:--1 --2 --3 --1,2 --1,3 --2,3 --1,2,3
Create PROCEDURE [dbo].[Arrange]
@input varchar(100)
AS
BEGIN
SET NOCOUNT ON
if(len(@input)<1) return
DECLARE @tableArrange Table
(
id int identity(1,1),
arrangeValue varchar(100),
maxid int,
lenOfValue int
)
declare @split varchar(10)
set @split = ','
declare @startIndex int
set @startIndex = 1
declare @endIndex int
set @endIndex = charindex(@split,@input,@startIndex)
declare @items varchar(100)
while(@endIndex<>0)
begin
set @items = Substring(@input,@startIndex,@endIndex-@startIndex)
if len(@items)>0
insert into @tableArrange(arrangeValue) values(@items)
set @startIndex = @endIndex + 1
set @endIndex = charindex(@split,@input,@startIndex)
end
set @items = Substring(@input,@startIndex,len(@input)-@startIndex+1 )
if len(@items)>0 insert into @tableArrange(arrangeValue) values(@items)
update @tableArrange set maxid=id,lenOfValue=1
declare @count int--
declare @currentlen int
declare @value varchar(100)
declare @valueInsert varchar(100)
declare @start int
declare @end int
declare @i int
declare @j int
declare @maxid int
declare @lenofvalue int
select @count = max(id) from @tableArrange
set @currentlen=1
while(@currentlen<@count)
begin
select @start=min(id),@end=max(id) from @tableArrange where lenOfValue=@currentlen
set @i=@start
while(@i<@end)
begin
select @maxid=maxid,@value=arrangeValue,@lenofvalue=lenofvalue from @tableArrange where id=@i
set @j=@maxid+1
while(@j<=@count)
begin
select @valueInsert=@value+','+arrangeValue from @tableArrange where id=@j
insert into @tableArrange(arrangeValue,maxid,lenofvalue)
values(@valueInsert,@j,@lenofvalue+1)
set @j=@j+1
end
set @i=@i+1
end
set @currentlen=@currentlen+1
end
select arrangeValue from @tableArrange
SET NOCOUNT ON
END
go
exec [Arrange] '1,2,3,4'
go
drop proc [Arrange]
go
/*
arrangeValue
----------------------------------------------------------------------------------------------------
1
2
3
4
1,2
1,3
1,4
2,3
2,4
3,4
1,2,3
1,2,4
1,3,4
2,3,4
1,2,3,4
*/
- ------------------------MSSQL中排列组合的实现------------------------------------
- 排列组合的实现
- 排列组合的实现
- 排列组合的实现
- 排列组合的递归实现
- 回溯实现数字的排列组合
- 排列组合数列的java实现
- 排列组合算法的递归实现
- C语言排列组合的实现
- js实现重复的排列组合
- 递归—排列组合的实现
- 排列组合算法的实现代码
- Java中简单的排列组合
- MSSQL中实现领导查询
- MSSQL 中双重循环实现发货数量的拆分
- 排列组合实现
- 用java实现对数组的排列组合
- 回溯实现数组元素的排列组合
- [Android实例] android动画效果切换案例
- [Android实例] Android activity动画跳转案例分析
- 各种音视频编解码学习详解 h264 ,mpeg4 ,aac 等所有音视频格式
- linux 线程安全 mutex 锁的使用
- Android 动画
- ------------------------MSSQL中排列组合的实现------------------------------------
- poj2455 Secret Milking Machine 网络流 最大流 二份答案
- HDU-1242 Rescue
- android_launcher的源码详细分析
- Android动画入门
- win8开发应用之二:管理应用生命周期和状态(使用 C# 和 XAML 的 Metro 风格应用)
- IOS开发之百度地图API应用
- 关于java和php的sign加密问题
- Objective-C研究院之数组对象(七)