0

我有一张桌子!

CREATE TABLE [dbo].[XmlTable](
    [XmlId] [int] IDENTITY(1,1) NOT NULL,
    [XmlDocument] [xml] NOT NULL,
 CONSTRAINT [PK_XmlTable] PRIMARY KEY CLUSTERED 
(
    [XmlId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]


GO

带有模式结构:存储在 xml 列中

    <dev:Doc xmlns:dev="http://www.w3.org/2001/XMLSchema" SchemaVersion="0.1" Settings="Testing" Title="Ordering">
  <dev:Base RevisionNumber="0" Baseid="34433" />
  <dev:Rev Time="2013-01-21T15:08:00">
    <dev:Person Name="Me" Systemid="54654" />
  </dev:Rev>
  <dev:Functions Id="A1">
    <dev:A1 Number="1">
      <dev:Codes>D</dev:Codes>
      <dev:Required>true</dev:Required>
      <dev:Informational>false</dev:Informational>
      <dev:Visitors>
        <dev:Visitor Name="Dev01" Location="STLRF">
          <dev:Divisions>
            <dev:Division Number="1" Name="TFR3" Usage="Monitor">
              <dev:Description>Development Fundamentals</dev:Description>
            </dev:Division>
            <dev:Division Number="2" Name="DEF32" Usage="Monitor">
              <dev:Description>Testing Fundamentals</dev:Description>
            </dev:Division>
            <dev:Division Number="3" Name="DEP13" Usage="None">
              <dev:Description>Guided Fundamentals</dev:Description>
            </dev:Division>
          </dev:Divisions>
        </dev:Visitor>
        <dev:Visitor Name="Dev02" Location="STLRF">
          <dev:Divisions>
            <dev:Division Number="1" Name="TFR3" Usage="Monitor">
              <dev:Description>Development Fundamentals</dev:Description>
            </dev:Division>
            <dev:Division Number="2" Name="DEF32" Usage="Monitor">
              <dev:Description>Testing Fundamentals</dev:Description>
            </dev:Division>
            <dev:Division Number="3" Name="DEP13" Usage="None">
              <dev:Description>Guided Fundamentals</dev:Description>
            </dev:Division>
          </dev:Divisions>
        </dev:Visitor>
        <dev:Visitor Name="Dev03" Location="FGRTY">
          <dev:Divisions>
            <dev:Division Number="1" Name="TFR3" Usage="Monitor">
              <dev:Description>Development Fundamentals</dev:Description>
            </dev:Division>
            <dev:Division Number="2" Name="DEF32" Usage="Monitor">
              <dev:Description>Testing Fundamentals</dev:Description>
            </dev:Division>
            <dev:Division Number="3" Name="DEP13" Usage="None">
              <dev:Description>Guided Fundamentals</dev:Description>
            </dev:Division>
          </dev:Divisions>
        </dev:Visitor>
      </dev:Visitors>
      <dev:Senders>
        <dev:Sender Name="FGY(14A)" />
      </dev:Senders>
    </dev:A1>
  </dev:Functions>
  <dev:Functions Id="A2">
    <dev:A2 Number="1">
      <dev:Codes>C</dev:Codes>
      <dev:Required>true</dev:Required>
      <dev:Informational>false</dev:Informational>
      <dev:Remarks>Support</dev:Remarks>
      <dev:Notes>Ready</dev:Notes>
      <dev:Visitors>
        <dev:Visitor Name="GHFF">
          <dev:Divisions>
            <dev:Division Number="0" Name="Trial" Usage="None">
              <dev:FromLocation>LOPO</dev:FromLocation>
              <dev:ToLocation>RDSS</dev:ToLocation>
              <dev:Description>Rich Filter</dev:Description>
            </dev:Division>
          </dev:Divisions>
        </dev:Visitor>
      </dev:Visitors>
      <dev:Senders>
        <dev:Sender Name="W33R" />
      </dev:Senders>
      <dev:IsReady>true</dev:IsReady>
      <dev:IsCall>false</dev:IsCall>
    </dev:A2>
    <dev:A2 Number="2">
      <dev:Codes>A</dev:Codes>
      <dev:Required>true</dev:Required>
      <dev:Informational>false</dev:Informational>
      <dev:Remarks>Loader Ready</dev:Remarks>
      <dev:Notes>Ready</dev:Notes>
      <dev:Visitors>
        <dev:Visitor Name="UDT">
          <dev:Divisions>
            <dev:Division Number="0" Name="Trial" Usage="None">
              <dev:FromLocation>TYUJ</dev:FromLocation>
              <dev:ToLocation>DETF</dev:ToLocation>
              <dev:Description>Web Enhance</dev:Description>
            </dev:Division>
          </dev:Divisions>
        </dev:Visitor>
      </dev:Visitors>
      <dev:Senders>
        <dev:Sender Name="RJ4" />
      </dev:Senders>
      <dev:IsReady>true</dev:IsReady>
      <dev:IsCall>false</dev:IsCall>
    </dev:A2>
  </dev:Functions>
</dev:Doc>

我正在尝试返回修订号、函数 ID、编号、访问者、访问者的位置、发件人姓名、

就像是:

RevNumber Function Id  Number  Visitor Location Sender
========= =========== ======== ======= ======== ======
  0          A1          1      Dev01   STLRF   FGY(14A)
  0          A1          1      Dev02   STLRF   FGY(14A)
  0          A1          1      Dev03   FGRTY   FGY(14A)
  0          A2          1      GHFF    NULL    W33R
  0          A2          2      UDT     NULL    RJ4

这是我在表格中的插入

INSERT INTO XmlTable(XMLDocument)
SELECT * FROM OPENROWSET(
   BULK 'C:\Users\123\Desktop\Practice.xml',
   SINGLE_BLOB) AS x;

我已经使用查询和值方法来拉回部分列表,但现在我被卡住了。我很感激任何帮助。谢谢!

这是查询

;WITH XMLNAMESPACES ( DEFAULT 'http://www.w3.org/2001/XMLSchema' )
SELECT  Visitor.query('.') AS XmlFragment,
        Visitor.value('@Name', 'NVARCHAR(MAX)') AS VisitorName,
        Visitor.value('@Location', 'NVARCHAR(MAX)') AS Location
FROM    XMLTable 
        CROSS APPLY XmlDocument.nodes('/Doc/Functions/A1/Visitors/Visitor') XD ( Visitor );
4

2 回答 2

2

您走在正确的轨道上 - 您只需添加一秒钟即可获取XML 节点CROSS APPLY内的节点列表:<Visitor>

;WITH XMLNAMESPACES ( DEFAULT 'http://www.w3.org/2001/XMLSchema' )
SELECT  
    Divisions.value('@Number', 'INT'),
    Divisions.value('@Name', 'VARCHAR(50)'),
    Divisions.value('@Usage', 'VARCHAR(100)'),
    Visitor.value('@Name', 'NVARCHAR(MAX)') AS VisitorName,
    Visitor.value('@Location', 'NVARCHAR(MAX)') AS Location
FROM
    @XmlTable
CROSS APPLY 
    XmlDocument.nodes('/Doc/Functions/A1/Visitors/Visitor') AS XD(Visitor)
CROSS APPLY 
    Visitor.nodes('Divisions/Division') AS XD2(Divisions)

返回如下输出:

在此处输入图像描述

于 2013-01-30T05:53:16.200 回答
0

我已经走得更远了,但是 A2 的数字没有显示,并且发件人返回 null。

;WITH XMLNAMESPACES ( 'http://www.w3.org/2001/XMLSchema' as dev )
SELECT  
    Document.value('@Title' , 'NVARCHAR(MAX)') Title,
    Functions.value('@Id', 'NVARCHAR(MAX)') Functions,
    A1.value('@Number', 'INT') Number,
    Visitor.value('@Name', 'NVARCHAR(MAX)') AS VisitorName,
    Visitor.value('@Location', 'NVARCHAR(MAX)') AS Location,
    Sender.value('@Name', 'NVARCHAR(MAX)') As Sender
FROM
    XmlTable

CROSS APPLY 
    xmlDocument.nodes('dev:Doc') As XD(Document)
CROSS APPLY 
    Document.nodes('dev:Functions') As XD2(Functions)
Outer APPLY
    Functions.nodes('dev:A1') As XD3(A1)
OUTER APPLY
    A1.nodes('dev:Visitors/dev:Visitor') As XD4(Visitor)
OUTER APPLY
    Visitor.nodes('dev:Senders/dev:Sender') As XD5(Sender)

在此处输入图像描述

于 2013-01-30T16:48:34.620 回答