Please start any new threads on our new 
    site at https://forums.sqlteam.com.  We've got lots of great SQL Server
    experts to answer whatever question you can come up with.
    
        | 
                
                    | 
                            
                                | Author | Topic |  
                                    | PHKStarting Member
 
 
                                        9 Posts | 
                                            
                                            |  Posted - 2009-07-31 : 15:20:24 
 |  
                                            | Here is my first try at a loop, it is not working and would appreciate any assistance. I want to make sure every userid is unique and if not add a digit to it.DECLARE @i INTDECLARE @id VARCHAR(64)DECLARE @newid VARCHAR(64)DECLARE @count INTSET @id = 'this is the user id'SET @newid = @idSET @i = 0SELECT @count = 0 WHILE 1 = 1BEGINSELECT @count = (SELECT COUNT(*) FROM usertest WHERE userid = @newid)IF @count = 0EXEC('INSERT INTO usertest (userid) VALUES (' + @newid +')')BREAKELSESET @i = @i + 1;SET @newid = @id+@i;CONTINUEEND I am receiving a Syntax error near ELSEPosted this in 2008 forum at first and then realized I was on the 2000 server instead of my 2008 serverAny help would be appreciated,Thank youPHK |  |  
                                    | ZathConstraint Violating Yak Guru
 
 
                                    298 Posts | 
                                        
                                          |  Posted - 2009-07-31 : 15:39:08 
 |  
                                          | Just a question, why add a digit to it.Why not have the user choose a different one if it exists? IF NOT EXISTS(SELECT FROM usertest WHERE userid = @newid)   INSERT INTO usertest (userid) VALUES (@newid)ELSERETURN 0Get info back from the sp to alert the user.Zath |  
                                          |  |  |  
                                    | PHKStarting Member
 
 
                                    9 Posts | 
                                        
                                          |  Posted - 2009-07-31 : 15:45:04 
 |  
                                          | I agree completely with you and like the idea, unfortunatily it was passed down to me that this is the way the organization wants to do it.  I can try one last ditch effort to do this, but I also have 20,000 individuals that are about to be users that I am giving these userids to.I created the userids yesterday by combining year signed up with last name and first name first initial and middle initial.Ex. 09jonesdw.  Now I am just making sure none of the 20,000 individuals I have in the database are the same and if they are to add that digit. |  
                                          |  |  |  
                                |  |  |  |