'Make a loop to separate each rows separately into one XML

I activated SQL Server broker
I deal with XML in query tables it is working well
I encountered a problem sometimes, it sends two row together in one XML as the following example:

declare @XML xml = '<row TableName="tblUsers" Action="INSERT" ID="1" AccountName="test1" />
                    <row TableName="tblUsers" Action="UPDATE" ID="2" AccountName="test2" />'

DECLARE @count INT;
SET @count = 1;

WHILE @count<= (select Count(*) from 
@XML.nodes('/*') T(c))
BEGIN
   PRINT @count
   // How do get the first row here and then the next
   SET @count = @count + 1;
END;

How can I make a loop in SQL Server to separate each row separately so that I can deal with this row, then the next, and then the next



Solution 1:[1]

declare @XML xml = '<row TableName="tblUsers" Action="INSERT" ID="1" AccountName="test1" />
                <row TableName="tblUsers" Action="UPDATE" ID="2" AccountName="test2" />'

DECLARE @count INT;
SET @count = 1;

WHILE @count<= (select Count(*) from @XML.nodes('/*') T(c))
BEGIN
   --PRINT @count
   select _XML
   from
   (
     SELECT  ROW_NUMBER() OVER (ORDER BY T.a) as _ROW ,a.query('.') as _XML  from @XML.nodes('/*') T(a)
   )a where _ROW = @count

   SET @count = @count + 1;
END;

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ???? ???