日期:2014-05-18  浏览次数:20668 次

如何得到两位字母或数字的所有组合?
(A...Z)(0...9)           比如   AB、C1、45。。。

------解决方案--------------------
declare @s1 as varchar(26)
declare @s2 as varchar(10)
set @s1 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ '
set @s2 = '0123456789 '
set @i as int
set @j as int
--1、字母两两组合
set @i = 1
set @j = 1
while @i <= 26
begin
set @j = @i + 1
while @j <= 26
begin
print substring(@s1 , @i , 1) + substring(@s1 , @j , 1)
@j = @j + 1
end
@i = @i + 1
end

--2、数字两两组合
set @i = 1
set @j = 1
while @i <= 10
begin
set @j = @i + 1
while @j <= 10
begin
print substring(@s2 , @i , 1) + substring(@s2 , @j , 1)
@j = @j + 1
end
@i = @i + 1
end

--3、字母和数字的混合组合
set @i = 1
set @j = 1
while @i <= 26
begin
set @j = 1
while @j <= 10
begin
print substring(@s1 , @i , 1) + substring(@s2 , @j , 1)
@j = @j + 1
end
@i = @i + 1
end

------解决方案--------------------
declare @s1 as varchar(26)
declare @s2 as varchar(10)
set @s1 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ '
set @s2 = '0123456789 '
declare @i as int
declare @j as int
--1、字母两两组合
set @i = 1
set @j = 1
while @i <= 26
begin
set @j = @i + 1
while @j <= 26
begin
print substring(@s1 , @i , 1) + substring(@s1 , @j , 1)
set @j = @j + 1
end
set @i = @i + 1
end

--2、数字两两组合
set @i = 1
set @j = 1
while @i <= 10
begin
set @j = @i + 1
while @j <= 10
begin
print substring(@s2 , @i , 1) + substring(@s2 , @j , 1)
set @j = @j + 1
end
set @i = @i + 1
end

--3、字母和数字的混合组合
set @i = 1
set @j = 1
while @i <= 26
begin
set @j = 1
while @j <= 10
begin
print substring(@s1 , @i , 1) + substring(@s2 , @j , 1)
set @j = @j + 1
end
set @i = @i + 1
end

/*
AB
AC
AD
AE
AF
AG
AH
AI
AJ
AK
AL
.
.
.
Z5
Z6
Z7
Z8
Z9
*/
------解决方案--------------------
declare @char varchar
declare @int int
declare @begin int
declare @end int
declare @i int
set @i = 1
set @char = 'A '
set @int=1
set @begin =ascii(@char)
set @end = ascii( 'Z ')
while @begin <= @end
begin
while @i <10
begin
print (char(str(@begin))+ str(@i))
set @i=@i +1
end
set @i=1
set @begin =@begin + 1
set @char = char(@begin)
end


然后在加上一部分 字母循环 和数字循环的