Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在SQL 2005和SQL 2012上使用元素XSINIL时的不同行为_Sql_Sql Server_Xml - Fatal编程技术网

在SQL 2005和SQL 2012上使用元素XSINIL时的不同行为

在SQL 2005和SQL 2012上使用元素XSINIL时的不同行为,sql,sql-server,xml,Sql,Sql Server,Xml,我在SQL 2005(9.0.5057)和SQL 2012(11.0.3128)之间有一个遗留问题。在SQL 2005和SQL 2012上运行以下示例SQL查询时,我得到了不同的结果: select 0 'test1/@old', null 'test1', null 'test2/@old', 2 'test2', 2 'test3/@old', 2 'test3', null 'test4/@old', null 'test4' FOR XML PATH('Data'), E

我在SQL 2005(9.0.5057)和SQL 2012(11.0.3128)之间有一个遗留问题。在SQL 2005和SQL 2012上运行以下示例SQL查询时,我得到了不同的结果:

select
  0 'test1/@old', null 'test1',
  null 'test2/@old', 2 'test2',
  2 'test3/@old', 2 'test3',
  null 'test4/@old', null 'test4'
FOR XML PATH('Data'), ELEMENTS XSINIL
对于SQL 2005,结果是:

<Data xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <test1 old="0" />
  <test2>2</test2>
  <test3 old="2">2</test3>
  <test4 xsi:nil="true" />
</Data>
<Data xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <test1 old="0" xsi:nil="true" />
  <test2>2</test2>
  <test3 old="2">2</test3>
  <test4 xsi:nil="true" />
</Data>

2.
2.
对于SQL 2012,结果是:

<Data xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <test1 old="0" />
  <test2>2</test2>
  <test3 old="2">2</test3>
  <test4 xsi:nil="true" />
</Data>
<Data xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <test1 old="0" xsi:nil="true" />
  <test2>2</test2>
  <test3 old="2">2</test3>
  <test4 xsi:nil="true" />
</Data>

2.
2.
Test1是出现问题的地方。SQL 2012的行为是正确的,但我很难在SQL 2005中找到修补程序或解决方法

我在SQLServer2008R2(10.50.2500)上运行了相同的测试,但得到了SQLServer2005R2的结果

我是不是错过了什么

我是不是错过了什么

不,这似乎是SQL Server中的一个bug,在SQL Server 2012(可能是SP1)中已修复

我正努力在SQL中找到一个补丁或解决方法 2005年

您可以接受的一种解决方法是将缺少的属性添加到应该添加的位置

将查询结果存储在XML变量中,然后使用and

一次只能添加一个值,因此必须在循环中使用检查缺少
xsi:nil
属性的空节点来完成

declare@xxml
设置@X=(
选择
0'test1/@old',null'test1',
空'test2/@old',2'test2',
2'test3/@old',2'test3',
null'test4/@old',null'test4'
对于xml路径(“数据”),元素为xsini
)
而@X.exist('/*[空(text())和空(*)以及空(@xsi:nil)])=1
set@X.modify('declare namespace xsi='http://www.w3.org/2001/XMLSchema-instance";
将(属性xsi:nil{“true”})插入到
(//*[empty(text())和empty(*)和empty(@xsi:nil)])[1]')
选择@X
/*
提供XML中的所有节点。
括号
[]
用于谓词。
empty(text())
检查节点中的文本。
empty(*)
检查子节点。

empty(@xsi:nil)
搜索是否存在
xsi:nil
属性。

这正是我最终要做的。虽然不漂亮,但很管用。您知道modify方法是否有任何显著的性能缺陷吗。执行这项工作的存储过程每分钟都会被调用无数次。@RichardDorman不,我没有听说过任何具体的情况会非常糟糕,除了一般认为SQL Server中的XML速度很慢之外。如果这太慢,那么我想您可以在SQLServerCLR中执行一些操作。