Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/9.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 server 2008 查询未返回正确的结果--Sql Server Xquery_Sql Server 2008_Tsql_Xquery_Xquery Sql - Fatal编程技术网

Sql server 2008 查询未返回正确的结果--Sql Server Xquery

Sql server 2008 查询未返回正确的结果--Sql Server Xquery,sql-server-2008,tsql,xquery,xquery-sql,Sql Server 2008,Tsql,Xquery,Xquery Sql,我的示例Xml如下所示: <tst:Root xmlns:tst="http://tempuri.org/some.xsd" SchemaVersion="0.2.1234.12345" RevNumber="1"> <tst:stuff someID="1">This is <tst:comment>1st.</tst:comment> what I <tst:comment>2nd</tst:comment>just

我的示例Xml如下所示:

<tst:Root xmlns:tst="http://tempuri.org/some.xsd" SchemaVersion="0.2.1234.12345" RevNumber="1">
  <tst:stuff someID="1">This is <tst:comment>1st.</tst:comment> what I <tst:comment>2nd</tst:comment>just want to show</tst:stuff>
</tst:Root>
 -- Set up the XML table to demonstrate
 declare @table table
 (
      ID int,
      xmlDoc  xml
 )

 -- insert your example XML
 insert into @table 
 Values (1, '<tst:Root xmlns:tst="http://tempuri.org/some.xsd" SchemaVersion="0.2.1234.12345" RevNumber="1"><tst:stuff someID="1">This is <tst:comment>1st.</tst:comment> what I <tst:comment>2nd</tst:comment>just want to show</tst:stuff></tst:Root>');

 -- Do the query
 with xmlnamespaces (Default 'http://tempuri.org/some.xsd')
 select ID, xmlDoc.query('//Root/stuff/text()') as SampleXml
 from @table
这是一个返回一切:这是第一。我想展示的是什么

WITH XMLNAMESPACES (Default 'http://tempuri.org/some.xsd')
SELECT     

    a.value('.', 'nvarchar(100)') as SampleXml
    From
    XmlTable As x
    Cross Apply XmlDoc.nodes('Root/stuff') a(a)
如何排除注释节点,但仍能获得正确的结果

编辑:

我想像这样归还:

<tst:Root xmlns:tst="http://tempuri.org/some.xsd" SchemaVersion="0.2.1234.12345" RevNumber="1">
  <tst:stuff someID="1">This is <tst:comment>1st.</tst:comment> what I <tst:comment>2nd</tst:comment>just want to show</tst:stuff>
</tst:Root>
 -- Set up the XML table to demonstrate
 declare @table table
 (
      ID int,
      xmlDoc  xml
 )

 -- insert your example XML
 insert into @table 
 Values (1, '<tst:Root xmlns:tst="http://tempuri.org/some.xsd" SchemaVersion="0.2.1234.12345" RevNumber="1"><tst:stuff someID="1">This is <tst:comment>1st.</tst:comment> what I <tst:comment>2nd</tst:comment>just want to show</tst:stuff></tst:Root>');

 -- Do the query
 with xmlnamespaces (Default 'http://tempuri.org/some.xsd')
 select ID, xmlDoc.query('//Root/stuff/text()') as SampleXml
 from @table
有没有比以下方法更简单的方法:

WITH XMLNAMESPACES (Default 'http://tempuri.org/some.xsd')
SELECT     

a.value('text()[1]', 'nvarchar(100)') + a.value('text()[2]', 'nvarchar(100)') + a.value('text()[3]', 'nvarchar(100)')  as SampleXml

From
XmlTable As x
Cross Apply XmlDoc.nodes('Root/stuff') a(a)
试试这个:

 declare @xml xml;

 set @xml = convert(xml, 'your XML');

 with xmlnamespaces (Default 'http://tempuri.org/some.xsd')
 select @xml.query('//stuff/text()')
假设您有一个包含XML列的表,它将如下所示:

<tst:Root xmlns:tst="http://tempuri.org/some.xsd" SchemaVersion="0.2.1234.12345" RevNumber="1">
  <tst:stuff someID="1">This is <tst:comment>1st.</tst:comment> what I <tst:comment>2nd</tst:comment>just want to show</tst:stuff>
</tst:Root>
 -- Set up the XML table to demonstrate
 declare @table table
 (
      ID int,
      xmlDoc  xml
 )

 -- insert your example XML
 insert into @table 
 Values (1, '<tst:Root xmlns:tst="http://tempuri.org/some.xsd" SchemaVersion="0.2.1234.12345" RevNumber="1"><tst:stuff someID="1">This is <tst:comment>1st.</tst:comment> what I <tst:comment>2nd</tst:comment>just want to show</tst:stuff></tst:Root>');

 -- Do the query
 with xmlnamespaces (Default 'http://tempuri.org/some.xsd')
 select ID, xmlDoc.query('//Root/stuff/text()') as SampleXml
 from @table
这就是我的结果:


谢谢你的回复,我希望它是一行而不是3行!我可以通过我发布的最新查询得到它,我只是想知道是否有更好的方法!这似乎不是最好的办法。对良好的反应投了赞成票。再次感谢!它可以比你做的更简单。假设您的XML文档是记录中的一个字段,您根本不需要使用交叉应用,除非您想做其他您没有提到的事情。有关示例,请参见我的更新答案。