Sql server 2012 SQLServerXQuery始终包含两个字段

Sql server 2012 SQLServerXQuery始终包含两个字段,sql-server-2012,xquery-sql,Sql Server 2012,Xquery Sql,我只想从xml字段的一列中获取一些数据: <CDirData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://Fnet.ESB.Schemas.CentroDirectivo.CDirData"> <ProcedureData xmlns=""> <ProcedureId>

我只想从xml字段的一列中获取一些数据:

<CDirData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://Fnet.ESB.Schemas.CentroDirectivo.CDirData">
  <ProcedureData xmlns="">
    <ProcedureId>7001</ProcedureId>
    <CentroDirectivo>Subsecretaria</CentroDirectivo>
  </ProcedureData>
  <SolicitudData xmlns="">
...
但始终返回字段concat

我只需要第一个


提前谢谢

将XPath表达式放在括号中,并添加[1]以仅获取组中的第一个元素

DECLARE @x XML = '<CDirData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://Fnet.ESB.Schemas.CentroDirectivo.CDirData">
  <ProcedureData xmlns="">
    <ProcedureId>7001</ProcedureId>
    <CentroDirectivo>Subsecretaria1</CentroDirectivo>
  </ProcedureData>
  <ProcedureData xmlns="">
    <ProcedureId>7002</ProcedureId>
    <CentroDirectivo>Subsecretaria2</CentroDirectivo>
  </ProcedureData>
  <ProcedureData xmlns="">
    <ProcedureId>7003</ProcedureId>
    <CentroDirectivo>Subsecretaria3</CentroDirectivo>
  </ProcedureData>
  <ProcedureData xmlns="">
    <ProcedureId>7004</ProcedureId>
    <CentroDirectivo>Subsecretaria4</CentroDirectivo>
  </ProcedureData>
  </CDirData>'

SELECT
    @x.query('(//*[local-name()="ProcedureId"])[1]').value('.','nvarchar(max)') as R
DECLARE@xxml='1〕
7001
地下室1
7002
秘书处下2
7003
秘书处下3
7004
地下室4
'
挑选
@x、 查询('(/*[local-name()=“ProcedureId”])[1]')。值('.','nvarchar(max)')为R

非常有用,谢谢大家!
DECLARE @x XML = '<CDirData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://Fnet.ESB.Schemas.CentroDirectivo.CDirData">
  <ProcedureData xmlns="">
    <ProcedureId>7001</ProcedureId>
    <CentroDirectivo>Subsecretaria1</CentroDirectivo>
  </ProcedureData>
  <ProcedureData xmlns="">
    <ProcedureId>7002</ProcedureId>
    <CentroDirectivo>Subsecretaria2</CentroDirectivo>
  </ProcedureData>
  <ProcedureData xmlns="">
    <ProcedureId>7003</ProcedureId>
    <CentroDirectivo>Subsecretaria3</CentroDirectivo>
  </ProcedureData>
  <ProcedureData xmlns="">
    <ProcedureId>7004</ProcedureId>
    <CentroDirectivo>Subsecretaria4</CentroDirectivo>
  </ProcedureData>
  </CDirData>'

SELECT
    @x.query('(//*[local-name()="ProcedureId"])[1]').value('.','nvarchar(max)') as R