create procedure sp_Sep(@a varchar(10))
as
begin
declare @count_1 int
declare @count_2 int
create table #temp
(
id int identity(1,1),
unit char(1)
)
set @count_1=len(@a)
set @count_2=1
declare @mysql varchar(2000)
set @mysql='declare @number'+convert(varchar,@count_2)+' nchar(1) '
while @count_2<@count_1
begin
set @count_2=@count_2+1
set @mysql=@mysql+'declare @number'+convert(varchar,@count_2)+' nchar(1) '
end
set @mysql=@mysql+'declare @a varchar(10) set @a='''+convert(varchar,@a)+''''
set @count_2=1
while @count_2<=@count_1
begin
set @mysql=@mysql+' set @number'+convert(varchar,@count_2)+'=substring(@a,'+convert(varchar,@count_2)+',1)'
set @count_2=@count_2+1
end
set @count_2=1
while @count_2<=@count_1
begin
set @mysql=@mysql+'insert into #temp(unit) select @number'+convert(varchar,@count_2)+' '
set @count_2=@count_2+1
end
set @mysql=@mysql+'select * from #temp'
exec(@mysql)
drop table #temp
end
功能并不复杂,可以用于分解字符串(够无聊吧)
测试时可以用下面语句
exec sp_Sep 'a1c2b3'
由于在Sp内作了限定,只能分解10个字符以下的字符串(要更长的话自己做点小改动就是了)
本来做这个东西是看CSDN上有人要一个能给出不定长字符串全排列的SQL语句时想到的(那个BT),但是全排列的逻辑好像不是T-SQL能完成的,放弃了,这个算是副产品吧