查询XML文件上传到SQL Server中的单个列

时间:2013-01-21 20:55:03

标签: sql sql-server sql-server-2008 tsql xquery

我正在尝试在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;

2 个答案:

答案 0 :(得分:0)

结帐value()nodes()

答案 1 :(得分:0)

SQL Fiddle

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)

<强> Results

| 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 |