The proper way of looping trough records in SQL is using a
CURSOR like this:
DECLARE mycursor CURSOR FOR
select A.B.value('(USER_ID)[1]', 'nvarchar(30)' ) [USER_ID],
A.B.value('(USER_NAME)[1]', 'nvarchar(30)' ) [USER_NAME]
from @l_n_XMLDoc.nodes('//ROW') as A(B)
DECLARE @USER_ID NVARCHAR(30)
DECLARE @USER_NAME NVARCHAR(255)
OPEN mycursor
FETCH NEXT FROM mycursor INTO @USER_ID, @USER_NAME
WHILE @@FETCH_STATUS = 0
BEGIN
FETCH NEXT FROM mycursor INTO @USER_ID, @USER_NAME
END
CLOSE mycursor
DEALLOCATE mycursor
I would further suggest you read about the
MERGE statement. Perhaps you can rewrite the whole loop including the inserts and updates using that.