Afficher un message
Vieux 24/03/2008, 21h32   #2
Plamen Ratchev
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Selecting alphanumeric combinations that do not exist already in table?????

That will not be a fast check. There are 26 letters in the English alphabet
(assuming we count lower case). There are 10 digits. The number of
combinations for a 6 position user name is (number_of_chars ^ length), or in
this case (36 ^ 6), which is 2,176,782,336 combinations. If you add the
upper case letters, then there are 56,800,235,584 combinations (62 ^ 6).

Here is one method to generate all possible combinations. It is for all
lower case letters and digits. You can uncomment the OR condition to add the
upper case letters. And this does not make any assumptions that the names
may start with letter only, etc.

CREATE TABLE Chars (c CHAR(1))

INSERT INTO Chars (c)
SELECT CHAR(number)
FROM master..spt_values
WHERE type = 'P'
AND (number BETWEEN 48 AND 57
-- OR number BETWEEN 65 AND 90
OR number BETWEEN 97 AND 122)

CREATE TABLE Users (usr CHAR(6))

INSERT INTO Users (usr)
SELECT I.c + II.c + III.c + IV.c + V.c + VI.c
FROM Chars AS I
CROSS JOIN Chars AS II
CROSS JOIN Chars AS III
CROSS JOIN Chars AS IV
CROSS JOIN Chars AS V
CROSS JOIN Chars AS VI

Then you can simply use the the EXCEPT operator in SQL Server 2005 or NOT
EXISTS to check against the list of existing user names and return only
those that are not in use.

HTH,

Plamen Ratchev
http://www.SQLStudio.com

  Réponse avec citation
 
Page generated in 0,04734 seconds with 9 queries