0

Trying to create a temp table where the AlphaSeq column would go through an "array" of alphanumeric characters that do not repeat and follow a flow as shown in the example below.

Example:

AlphaSeq NumericSeq
A 1
A1 2
B 3
B1 4
AA 5
AB 6
BA 7
BB 8
[ETC] [ETC]

Here's what I have so far (Obviously the "IG" is static as it is written now, so Im trying to make that field more fluid)

CREATE TABLE #Index 
(
     AlphaSeq VARCHAR(2), 
     NumericSeq TINYINT
)

DECLARE @ArrayNum AS TINYINT;
DECLARE @ReceiptSeq AS VARCHAR(2);

SET @ArrayNum = 0
SET @ReceiptSeq = 'IG'

WHILE @ArrayNum < 100
BEGIN 
    SET @ArrayNum = @ArrayNum + 1

    INSERT INTO #Index (AlphaSeq, NumericSeq)
    VALUES (@ReceiptSeq, @ArrayNum)
END

SELECT *
FROM #Index
1
  • I would suggest you get a list of all the charcters you need, and CROSS JOIN them to each other. Commented Aug 25, 2021 at 18:01

1 Answer 1

1

You can simply cross-join the list of characters

CREATE TABLE #Index 
(
     AlphaSeq VARCHAR(2) NOT NULL, 
     NumericSeq SMALLINT NOT NULL IDENTITY
);

WITH Chars AS (
    SELECT *
    FROM (VALUES
      ('A'),('B'),('C'),('D'),('E'),('F'),('G'),('H'),('I'),('J'),('K'),('L'),('M'),('N'),('O'),('P'),('Q'),('R'),('S'),('T'),('U'),('V'),('W'),('X'),('Y'),('Z')
    ) v(chr)
),
CharsWithNums AS (
    SELECT *
    FROM (VALUES
      ('1'),('2'),('3'),('4'),('5'),('6'),('7'),('8'),('9'),('0')
    ) v(chr)
    UNION ALL
    SELECT *
    FROM Chars
)
INSERT #Index (AlphaSeq)
SELECT chr AS AlphaSeq
FROM Chars
UNION ALL
SELECT a.chr + b.chr
FROM Chars a
CROSS JOIN CharsWithNums b
ORDER BY AlphaSeq;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.