我有2个类型的文件:
@xmlAuthors1 XML =N'
<Author name="John" surname="Clinton" />
<Author name="Bill" surname="Skobov" />
<Author name="John" surname="Lokwood" />;
@xmlAuthors2 XML =N'
<Author name="Bill" surname="Skobov" />
<Author name="John" surname="Clinton" />
<Author name="John" surname="Lokwood" />;
如何发现他们是平等的?比较文件1的每一行与文件2的每一行
答案 0 :(得分:3)
例如,您可以比较它们:
DECLARE @xmlAuthors1 XML = N'
<Author name="John" surname="Clinton" />
<Author name="Bill" surname="Skobov" />
<Author name="John" surname="Lokwood" />',
@xmlAuthors2 XML =N'
<Author name="Bill" surname="Skobov" />
<Author name="Johns" surname="Clinton" />
<Author name="John" surname="Lokwood" />'
SELECT *
FROM (
SELECT CAST(x1.t.query('.') as nvarchar(max)) as x1,
CAST(x2.t.query('.') as nvarchar(max)) as x2
FROM @xmlAuthors1.nodes('/*') as x1(t)
FULL OUTER JOIN @xmlAuthors2.nodes('/*') as x2(t)
ON CAST(x1.t.query('.') as nvarchar(max)) = CAST(x2.t.query('.') as nvarchar(max))
) as x
WHERE x1 is null or x2 is null
如果相等则没有输出
答案 1 :(得分:1)
这取决于你的平等意义。我会按如下方式比较数据:
DECLARE @xmlAuthors1 XML =N'
<Author name="John" surname="Clinton" />
<Author name="Bill" surname="Skobov" />
<Author name="John" surname="Lokwood" />
<Author name="John" surname="Blurred" />';
DECLARE @xmlAuthors2 XML =N'
<Author name="Bill" surname="Skobov" />
<Author name="John" surname="Clinton" />
<Author name="John" surname="Lokwood" />';
WITH Xml1 AS
(
SELECT
T.A.value('@name', 'varchar(20)') Name,
T.A.value('@surname', 'varchar(20)') Surname
FROM @xmlAuthors1.nodes('/Author') T(A)
), Xml2 AS
(
SELECT
T.A.value('@name', 'varchar(20)') Name,
T.A.value('@surname', 'varchar(20)') Surname
FROM @xmlAuthors2.nodes('/Author') T(A)
)
SELECT 'Unique in 1' [Description], * FROM (SELECT * FROM Xml1 EXCEPT SELECT * FROM Xml2) Q1
UNION ALL
SELECT 'Unique in 2' [Description], * FROM (SELECT * FROM Xml2 EXCEPT SELECT * FROM Xml1) Q2
这个特殊的例子产生:
Description Name Surname
----------- ---- -------
Unique in 1 John Blurred
答案 2 :(得分:1)
这是我的方法:
.query(.data()).value()
有一个很好的技巧可以将所有内部文本检索为空格分隔的字符串。这使得在字符串级别上进行比较变得容易......
我更改了您的输入以包含更多测试用例:
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)