這是我的方法:
有一個很好的技巧與.query(.data()).value()
檢索所有內部文本的空間分隔串。這可以很容易地在串級比較...
我改變了你輸入到inlcude一些測試案例:如果你想找到那些等於在這兩個你可以使用此
DECLARE @xmlAuthors1 XML = N'
<Author name="John" surname="Clinton" />
<Author name="Bill" surname="Different" />
<Author name="John" surname="Lokwood" />
<Author name="Test" surname="it" />',
@xmlAuthors2 XML =N'
<Author name="Bill" surname="Skobov" />
<Author name="John" surname="Clinton" />
<Author name="John" surname="Lokwood" />
<Author name="One" surname="More" />';
SELECT ISNULL(Author1,Author2) AS Author
,CASE WHEN Author1 IS NULL THEN 'Exists in 2' ELSE 'Exists in 1' END AS [Source]
FROM
(
SELECT B.query('data(@*)').value('.','varchar(max)') AS Author1
FROM @xmlAuthors1.nodes('/Author') AS A(B)
) AS tbl1
FULL OUTER JOIN
(
SELECT B.query('data(@*)').value('.','varchar(max)') As Author2
FROM @xmlAuthors2.nodes('/Author') AS A(B)
)AS tbl2 ON Author1=Author2
WHERE Author1 IS NULL OR Author2 IS NULL
SELECT B.query('data(@*)').value('.','varchar(max)') AS EqualAuthor
FROM @xmlAuthors1.nodes('/Author') AS A(B)
INTERSECT
SELECT B.query('data(@*)').value('.','varchar(max)')
FROM @xmlAuthors2.nodes('/Author') AS A(B)
見http://stackoverflow.com/questions/9013680/t-sql-how-can-i-compare-two-variables-of-type-xml-when-length-varcharmax – Serg