1

我正在尝试在 SQL Server 中学习 XQuery 和 Xpath

我创建了一个示例文件并将其上传到具有 2 列 ID、XMLDoc 的表中。以下代码位于 XMLDoc 列中的文档中,因此它是该列中的唯一记录。

我正在尝试查询该文件,以便它像普通的 select 语句一样在表中显示所有结果。您将如何构造 select 语句来选择像 select * 这样的所有信息?您将如何像所有供应商一样选择一个领域?我想为每个项目选择供应商、请求者。

这是xml:

     <tst:Document xmlns:tst ="http://www.w3.org/2001/XMLSchema" SchemaVersion="0.1" Classification="Test" UniqueIdentifier="1234" Title="Test">
  <tst:Revision RevNumber="0" TimeStamp="2013-01-21T12:56:00">
    <tst:Author Name="Me" Guid="1234" />
  </tst:Revision>
  <tst:Formats>
    <tst:A12 Item="1">
      <tst:Requestor Name="ADC" />
      <tst:Supplier Name="BBC" />
      <tst:Code>B</tst:Code>
      <tst:IsRequirement>true</tst:IsRequirement>
      <tst:IsNotRequired>false</tst:IsInformation>
      <tst:Remarks>ADC (Random Input Section)</tst:Remarks>
      <tst:Notes>Next Round.</tst:Notes>
      <tst:Events>
        <tst:SubTest Item="0">
          <tst:BLDG>BLDG1</tst:BLDG>
          <tst:BLDG2>BLDG2</tst:BLDG2>
          <tst:Function>Testing</tst:Function>
          <tst:Desciption>Normal Flow</tst:Desciption>
        </tst:SubTest>
      </tst:Events>
      <tst:IsReady>true</tst:IsReady>
      <tst:IsNotReady>false</tst:IsNotReady>
    </tst:A12>
    <tst:A12 Item="2">
      <tst:Requestor Name="ADC" />
      <tst:Supplier Name="BBC" />
      <tst:Code>A</tst:Code>
      <tst:IsRequirement>true</tst:IsRequirement>
      <tst:IsInformation>false</tst:IsInformation>
      <tst:Remarks>Requirement Not yet met.</tst:Remarks>
      <tst:Notes>Ready.</tst:Notes>
      <tst:Events>
        <tst:SubTest Item="0">
          <tst:BLDG>BLDG3</tst:BLDG>
          <tst:BLDG2>BLDG4</tst:BLDG2>
          <tst:TotalEvents>1</tst:TotalEvents>
          <tst:Function>Development</tst:Function>
          <tst:Desciption>Process Flow</tst:Desciption>
        </tst:SubTest>
      </tst:Events>
      <tst:IsReady>true</tst:IsReady>
      <tst:IsNotReady>false</tst:IsNotReady>
    </tst:A12>
  </tst:Formats>
</tst:Document>

我跑的查询

我刚得到一个回报,但它仍然以 xml 形式显示:

Select XMLDoc.query('/*/*/*/*[local-name()=("Requestor", "Supplier")]')
       From XMLLoad

我更新了 xml 片段,sry 有一个错字!现在将加载

INSERT INTO TableName(ColumnName)
SELECT * FROM OPENROWSET(
BULK 'C:\Users\Filepath.xml',
SINGLE_BLOB) AS x;
4

2 回答 2

0

SQL小提琴

MS SQL Server 2008 架构设置

create table XMLDoc (XMLLoad xml);

insert into XMLDoc(XMLLoad) values('
     <tst:Document xmlns:tst ="http://www.w3.org/2001/XMLSchema" SchemaVersion="0.1" Classification="Test" UniqueIdentifier="1234" Title="Test">
  <tst:Revision RevNumber="0" TimeStamp="2013-01-21T12:56:00">
    <tst:Author Name="Me" Guid="1234" />
  </tst:Revision>
  <tst:Formats>
    <tst:A12 Item="1">
      <tst:Requestor Name="ADC" />
      <tst:Supplier Name="BBC" />
      <tst:Code>B</tst:Code>
      <tst:IsRequirement>true</tst:IsRequirement>
      <tst:IsInformation>false</tst:IsInformation>
      <tst:Remarks>ADC (Random Input Section)</tst:Remarks>
      <tst:Notes>Next Round.</tst:Notes>
      <tst:Events>
        <tst:SubTest Item="0">
          <tst:BLDG>BLDG1</tst:BLDG>
          <tst:BLDG2>BLDG2</tst:BLDG2>
          <tst:Function>Testing</tst:Function>
          <tst:Desciption>Normal Flow</tst:Desciption>
        </tst:SubTest>
      </tst:Events>
      <tst:IsReady>true</tst:IsReady>
      <tst:IsNotReady>false</tst:IsNotReady>
    </tst:A12>
    <tst:A12 Item="2">
      <tst:Requestor Name="ADC" />
      <tst:Supplier Name="BBC" />
      <tst:Code>A</tst:Code>
      <tst:IsRequirement>true</tst:IsRequirement>
      <tst:IsInformation>false</tst:IsInformation>
      <tst:Remarks>Requirement Not yet met.</tst:Remarks>
      <tst:Notes>Ready.</tst:Notes>
      <tst:Events>
        <tst:SubTest Item="0">
          <tst:BLDG>BLDG3</tst:BLDG>
          <tst:BLDG2>BLDG4</tst:BLDG2>
          <tst:TotalEvents>1</tst:TotalEvents>
          <tst:Function>Development</tst:Function>
          <tst:Desciption>Process Flow</tst:Desciption>
        </tst:SubTest>
      </tst:Events>
      <tst:IsReady>true</tst:IsReady>
      <tst:IsNotReady>false</tst:IsNotReady>
    </tst:A12>
  </tst:Formats>
</tst:Document>');

查询 1

with xmlnamespaces('http://www.w3.org/2001/XMLSchema' as tst)
select A12.X.value('@Item', 'int') as A12,
       A12.X.value('tst:Requestor[1]/@Name', 'varchar(25)')        as Requestor,
       A12.X.value('tst:Supplier[1]/@Name', 'varchar(25)')         as Supplier,
       A12.X.value('(tst:Code/text())[1]', 'varchar(25)')          as Code,
       A12.X.value('(tst:IsRequirement/text())[1]', 'bit')         as IsRequirement,
       A12.X.value('(tst:IsInformation/text())[1]', 'bit')         as IsInformation,
       A12.X.value('(tst:Remarks/text())[1]', 'varchar(50)')       as Remarks,
       A12.X.value('(tst:Notes/text())[1]', 'varchar(50)')         as Notes,
       ST.X.value('@Item', 'int')                                  as SubTest,
       ST.X.value('(tst:BLDG/text())[1]', 'varchar(25)')           as BLDG,
       ST.X.value('(tst:BLDG2/text())[1]', 'varchar(25)')          as BLDG2,
       ST.X.value('(tst:TotalEvents/text())[1]', 'int')            as TotalEvents,
       ST.X.value('(tst:Function/text())[1]', 'varchar(25)')       as [Function],
       ST.X.value('(tst:Desciption/text())[1]', 'varchar(50)')     as Desciption
from XMLDoc as X
  cross apply X.XMLLoad.nodes('/tst:Document/tst:Formats/tst:A12') as A12(X)
  cross apply A12.X.nodes('tst:Events/tst:SubTest')                as ST(X)

结果

| A12 | REQUESTOR | SUPPLIER | CODE | ISREQUIREMENT | ISINFORMATION |                    REMARKS |       NOTES | SUBTEST |  BLDG | BLDG2 | TOTALEVENTS |    FUNCTION |   DESCIPTION |
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|   1 |       ADC |      BBC |    B |             1 |             0 | ADC (Random Input Section) | Next Round. |       0 | BLDG1 | BLDG2 |      (null) |     Testing |  Normal Flow |
|   2 |       ADC |      BBC |    A |             1 |             0 |   Requirement Not yet met. |      Ready. |       0 | BLDG3 | BLDG4 |           1 | Development | Process Flow |
于 2013-01-22T07:40:15.220 回答
0

查看value()nodes()

于 2013-01-21T21:58:20.853 回答