I have a MS SQL Table containing user data, e.g. Username and ID. This list should be compared with another list which is available as XML Document. Fortunately MS SQL 2005 is able to process XML so I searched around and found a post from Divya Agrawal on MSSQLTips.com. In Divyas tip 2 XML files are compared, so I modified it to compare XML and SQL row.
For this example, my SQL Table ‘Users’ has just 2 rows: “Name” (varchar(100)) and “ID” (int). My XML File looks like this:
<root> <data> <name>Thorsten Test</name> <id>123</id> </data> <data> <name>Peter Pommes</name> <id>124</id> </data> <data> <name>Wiki Pedia</name> <id>125</id> </data> </root>
So I need to check whether ‘root/data/id’ from XML file is already in table column ‘ID’. If not, the name and the ID should be returned. This could be done with this little query:
CREATE PROCEDURE [dbo].[FindNewUsers] @Userlist XML AS BEGIN SET NOCOUNT ON; Select N.value('name', 'varchar(100)') as Username, N.value('id', 'int') as ID from @Userlist.nodes('/root/data') as X(N) where N.value('id', 'int') Not in (select ID from Users) END