如何通过循环向表中插入唯一代码

时间:2014-03-29 04:39:28

标签: sql-server sql-server-2008 sql-server-2012

我需要在表格中插入单词。单词必须是唯一的。

像:

  AAAAA
  AAAAB
  AAAAC

  .....
  .....
  .....

  ZZZZZ

有没有简单的方法来实现这个?

3 个答案:

答案 0 :(得分:2)

declare     @a  int = 65,
      @b  int =  90,
      @a1  int = 65,
      @b1  int = 90,
      @a2  int = 65,
      @b2  int = 90

    while(@a<=@b)
    begin
    set @a1 = 65
    while(@a1<=@b1)
    begin
    set @a2 = 65
    while(@a2<=@b2)
    begin
    print char(@a) + char(@a1) +char(@a2)
    set @a2 = @a2+1
    end
    set @a1 = @a1+1
    end
    set @a = @a+1
    end

答案 1 :(得分:0)

这不是完整的代码。我故意不给你全部的东西。使用下面的代码开发自己的整个事物 -

select ASCII('A') as Ano
select ASCII('Z') as Zno

declare @count int = 1
while(@count <= 26)
    begin
    print @count
    set @count = @count + 1
    end

答案 2 :(得分:0)

这不是完全答案。

但是会给你一些关于这个

的想法
;WITH Alphabet AS 
(
    SELECT CHAR(65) AS Letter,CHAR(65) AS LetterNew, 65 AS Code
    UNION ALL
    SELECT CHAR(Code + 1),CHAR(Code),  Code + 1
    FROM Alphabet
    WHERE Code < 90
) 

SELECT Letter+Letter+Letter+Letter+LetterNew FROM Alphabet
UNION
SELECT Letter+Letter+Letter+LetterNew+Letter FROM Alphabet
UNION
SELECT Letter+Letter+LetterNew+Letter+Letter FROM Alphabet
UNION
SELECT Letter+LetterNew+Letter+Letter+Letter FROM Alphabet
UNION
SELECT LetterNew+Letter+Letter+Letter+Letter FROM Alphabet

<强> Fiddle Demo