I have a User table, which is having Userid(primary key), Username, Password
There is a stored procedure which accepts list of users as XML. My requirement is to insert the users to User table, but before inserting I need to check whether the Username is already exists. If exists then a random number should be appended with username.
How do i check whether username already exists or not and randomise it, below is the sql which blindly inserts the username. Any help?
XML Structure:<ROOT><User UserId="0" Username="ajohn" Password="548788844" ></ROOT>
SQL:
INSERT INTO [User] (Username, Password)
SELECT tab.col.value('@Username','nVarchar(max)') AS Username, tab.col.value('@Password','nVarchar(max)') AS Password,
FROM @pupilDetails.nodes('ROOT/User') tab(col)
Maybe this will help: