T-SQL:当长度>时如何比较两个XML类型的变量变量字符(最大)?
仅使用 SQL Server 2008 R2(这将位于存储过程中),如何确定 XML 类型的两个变量是否等效?
这就是我想做的:
DECLARE @XmlA XML
DECLARE @XmlB XML
SET @XmlA = '[Really long Xml value]'
SET @XmlB = '[Really long Xml value]'
IF @XmlA = @XmlB
SELECT 'Matching Xml!'
但你可能知道,它会返回:
消息 305,级别 16,状态 1,第 7 行 XML 数据类型不能为 比较或排序,除非使用 IS NULL 运算符。
我可以转换为 VarChar(MAX)
并进行比较,但这仅比较前 2MB。还有别的办法吗?
Using only SQL Server 2008 R2 (this is going to be in a stored proc), how can I determine if two variables of type XML are equivalent?
Here is what I want to do:
DECLARE @XmlA XML
DECLARE @XmlB XML
SET @XmlA = '[Really long Xml value]'
SET @XmlB = '[Really long Xml value]'
IF @XmlA = @XmlB
SELECT 'Matching Xml!'
But as you probably know, it returns:
Msg 305, Level 16, State 1, Line 7 The XML data type cannot be
compared or sorted, except when using the IS NULL operator.
I can convert to VarChar(MAX)
and compare, but that only compares the first 2MB. Is there another way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
比较两个 XML 文档的方法有很多种,很大程度上取决于您想要容忍哪种差异:您肯定需要容忍编码、属性顺序、无关紧要的空格、数字字符引用和属性分隔符的使用方面的差异,并且您可能还应该容忍注释、命名空间前缀和 CDATA 使用方面的差异。因此,将两个 XML 文档作为字符串进行比较绝对不是一个好主意 - 除非您首先调用 XML 规范化。
对于许多目的,XQuery deep-equals() 函数做了正确的事情(并且或多或少相当于比较两个 XML 文档的规范形式)。我对 Microsoft 的 SQL Server XQuery 实现了解不够,无法告诉您如何从 SQL 级别调用它。
There are many different ways of comparing two XML documents, and a lot depends on what kind of differences you want to tolerate: you definitely need to tolerate differences in encoding, attribute order, insignificant whitespace, numeric character references, and use of attribute delimiters, and you should probably also tolerate differences in use of comments, namespace prefixes, and CDATA. So comparing two XML documents as strings is definitely not a good idea - unless you invoke XML canonicalization first.
For many purposes the XQuery deep-equals() function does the right thing (and is more-or-less equivalent to comparing the canonical forms of the two XML documents). I don't know enough about Microsoft's SQL Server implementation of XQuery to tell you how to invoke this from the SQL level.
检查这个 SQL 函数:
这是源
函数无法比较 XML 片段,例如,当没有单个根元素时,例如:
为了解决此问题,当将 XML 传递给函数时,您必须将 XML 包装在
root
元素中或编辑函数来执行此操作。例如:Check this SQL function:
Here is the Source
The function fails to compare XML fragments e.g. when there is not a single root element, like:
In order to fix this, you must wrap your XMLs in
root
elements, when they are passed to the function or edit the function to do this. For, example:您可以将字段转换为 varbinary(max),对它们进行散列并比较散列。但是您肯定会错过 XML 是否等效但不相同
要计算哈希,您可以使用 CLR 函数:
或 sql 函数:
You may cast fields to varbinary(max), hash them and compare hashes. But you definitely miss if XMLs are equivalent but not identical
To calculate hash you may use either CLR function:
Or sql function:
如果您可以使用 SQL CLR,我建议使用 XNode.DeepEquals方法:
如果你不能,你可以编写自己的函数(参见SQL FIDDLE 示例):
If you can use SQL CLR, I suggest to write a function using XNode.DeepEquals Method:
If you cannot, you can write your own function (see SQL FIDDLE EXAMPLE):
我偶然发现 这篇相当全面的文章,其中更详细地实际比较 2 个 XML 条目的内容以确定它们是否相同。这是有道理的,因为节点中属性的顺序可以不同,即使它们的值完全相同。我建议您仔细阅读它,甚至实现该功能,看看它是否适合您...我很快就尝试了它,它似乎对我有用?
I stumbled upon this fairly comprehensive article which goes into more detail of actually comparing the CONTENT of 2 XML entries to determine whether they are the same. It makes sense, as the ordering of attributes in nodes CAN differ, even though their values are exactly the same. I'd recommend you read through it and even implement the function to see if it works for you... I tried it out quickly and it seemed to work for me?