Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/24.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.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 包含相同同级的XML的MSSQL解析_Sql Server_Xml_Tsql_Xpath_Xquery - Fatal编程技术网

Sql server 包含相同同级的XML的MSSQL解析

Sql server 包含相同同级的XML的MSSQL解析,sql-server,xml,tsql,xpath,xquery,Sql Server,Xml,Tsql,Xpath,Xquery,我试图查询一个包含XML的MSSQL数据库,并将其解析出来 以下是数据: <Root> <Relatives> <Relative> <Relation>Father</Relation> <BcAge>50</BcAge> <BilatAge>0</BilatAge> <OcAge>0</OcAge> </Relative> <R

我试图查询一个包含XML的MSSQL数据库,并将其解析出来

以下是数据:

<Root>
<Relatives>
<Relative>
  <Relation>Father</Relation>
  <BcAge>50</BcAge>
  <BilatAge>0</BilatAge>
  <OcAge>0</OcAge>
</Relative>
<Relative>
  <Relation>Mother</Relation>
  <BcAge>58</BcAge>
  <BilatAge>0</BilatAge>
  <OcAge>0</OcAge>
</Relative>

我怀疑你是否真的想同时得到你的结果。。。这不是有效的结果集,列名重复

如果您确实需要这样做,您可以这样做:

DECLARE @xml XML=
N'<Root>
<Relatives>
<Relative>
  <Relation>Father</Relation>
  <BcAge>50</BcAge>
  <BilatAge>0</BilatAge>
  <OcAge>0</OcAge>
</Relative>
<Relative>
  <Relation>Mother</Relation>
  <BcAge>58</BcAge>
  <BilatAge>0</BilatAge>
  <OcAge>0</OcAge>
</Relative>
</Relatives>
</Root>';
但可能您正在寻找的就是这个(任何计数的
):

选择相对值(N'(Relation/text())[1]',N'nvarchar(max')作为关系
,相对值(N'(BcAge/text())[1]',N'int')作为BcAge
,相对值(N'(BilatAge/text())[1]',N'int')作为BilatAge
,相对值(N'(OcAge/text())[1]',N'int')作为OcAge
从@xml.nodes(N'/Root/Relatives/Relative')作为(rel)
DECLARE @xml XML=
N'<Root>
<Relatives>
<Relative>
  <Relation>Father</Relation>
  <BcAge>50</BcAge>
  <BilatAge>0</BilatAge>
  <OcAge>0</OcAge>
</Relative>
<Relative>
  <Relation>Mother</Relation>
  <BcAge>58</BcAge>
  <BilatAge>0</BilatAge>
  <OcAge>0</OcAge>
</Relative>
</Relatives>
</Root>';