I am importing records from Excel and I want to avoid duplicates. In ASP Classic I have written a function that checks the database for duplicates. If it finds one it adds a number on the end of the username and checks again if the username is unique, e.g petejones becomes petejones1. Unfortunately this script is soooooo slow as the data base has around 150k records and it takes forever to search for uniqueness. Is there a way to do the same directly in SQL Server 2008 in T-SQL? so the whole process will be wicked quick. Is there a make unique process?
Here is the function in classic ASP.. I know there are better ways to do this, so dont laugh at my scripting.
FUNCTION CreateUniqueUsername(str)
SET DbConn = Server.CreateObject("ADODB.connection")
DbConn.Open DSN_LINK
nCounter = 0
Unique = ""
IF InStr(str, "@") > 0 THEN
strUsername = Left(str,InStr(str, "@")-1)
ELSE
strUsername = str
END IF
strUsername = FormatUsername(strUsername)
strSQL = "SELECT UserName FROM Member WHERE UserName = '" & strUsername & "';"
SET rs = DbConn.Execute(strSQL)
IF rs.EOF AND rs.BOF THEN
nFinalUsername = strUsername
ELSE
DO UNTIL Unique = true
nCounter = nCounter + 1
nFinalUsername = strUsername & nCounter
strSQL2 = "SELECT UserName FROM Member WHERE UserName = '" & nFinalUsername & " ' "
SET objRS = DbConn.Execute(strSQL2)
IF objRS.EOF THEN
Unique = true
ELSE
intCount = intCount
END IF
LOOP
objRS.Close
SET objRS = Nothing
END IF
rs.Close
SET rs = Nothing
SET DbConn = Nothing
CreateUniqueUsername = nFinalUsername
END FUNCTION
FUNCTION FormatUsername(str)
Dim OutStr
IF ISNULL(str) THEN EXIT FUNCTION
OutStr = lCase(Trim(str))
OutStr = Replace(OutStr, "’", "")
OutStr = Replace(OutStr, "”", "")
OutStr = Replace(OutStr, "'","")
OutStr = Replace(OutStr, "&","and")
OutStr = Replace(OutStr, "'", "")
OutStr = Replace(OutStr, "*", "")
OutStr = Replace(OutStr, ".", "")
OutStr = Replace(OutStr, ",", "")
OutStr = Replace(OutStr, CHR(34),"")
OutStr = Replace(OutStr, " ","")
OutStr = Replace(OutStr, "|","")
OutStr = Replace(OutStr, "&","")
OutStr = Replace(OutStr, "[","")
OutStr = Replace(OutStr, ";", "")
OutStr = Replace(OutStr, "]","")
OutStr = Replace(OutStr, "(","")
OutStr = Replace(OutStr, ")","")
OutStr = Replace(OutStr, "{","")
OutStr = Replace(OutStr, "}","")
OutStr = Replace(OutStr, ":","")
OutStr = Replace(OutStr, "/","")
OutStr = Replace(OutStr, "\","")
OutStr = Replace(OutStr, "?","")
OutStr = Replace(OutStr, "@","")
OutStr = Replace(OutStr, "!","")
OutStr = Replace(OutStr, "_","")
OutStr = Replace(OutStr, "''","")
OutStr = Replace(OutStr, "%","")
OutStr = Replace(OutStr, "#","")
FormatUsername = OutStr
END FUNCTION
Any assistance would be greatly appreciated as I am still learning SQL.
You could do this in SQL.
This looks for a matching name. If a match is found, then it gets the maximum number currently appended to it and adds one. So at most it does two SELECTS. Should be faster when there are lots of duplicates.