从XML文件中选择数据作为SQL中的表

时间:2011-10-04 14:15:45

标签: sql-server-2005 tsql

有人可以向我展示一些用于查询xml文件的TSQL,就好像它是一个表吗?

该文件位于服务器上,“C:\ xmlfile.xml”

并包含

<ArrayOfSpangemansFilter xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <SpangemansFilter>
        <FilterID>1219</FilterID>
        <Name>Fred</Name>
        <Code>510</Code>
        <Department>N</Department>
        <Number>305327</Number>
    </SpangemansFilter>
    <SpangemansFilter>
        <FilterID>3578</FilterID>
        <Name>Gary</Name>
        <Code>001</Code>
        <Department>B</Department>
        <Number>0692690</Number>
    </SpangemansFilter>
    <SpangemansFilter>
        <FilterID>3579</FilterID>
        <Name>George</Name>
        <Code>001</Code>
        <Department>X</Department>
        <Number>35933</Number>
    </SpangemansFilter>
</ArrayOfSpangemansFilter>

示例输出我在

之后
FilterID    |Name       |Code       |Department             |Number
-------------------------------------------------------------------
1219        |Fred       |510        |N                      |305327
3578        |Gary       |001        |B                      |0692690
3579        |George     |001        |X                      |35933

2 个答案:

答案 0 :(得分:25)

set @xmlData='<?xml version="1.0"?>
<ArrayOfSpangemansFilter xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<SpangemansFilter>
<FilterID>1219</FilterID>
<Name>Fred</Name>
<Code>510</Code>
<Department>N</Department>
<Number>305327</Number>
</SpangemansFilter>
<SpangemansFilter>
<FilterID>3578</FilterID>
<Name>Gary</Name>
<Code>001</Code>
<Department>B</Department>
<Number>0692690</Number>
</SpangemansFilter>
<SpangemansFilter>
<FilterID>3579</FilterID>
<Name>George</Name>
<Code>001</Code>
<Department>X</Department>
<Number>35933</Number>
</SpangemansFilter>
</ArrayOfSpangemansFilter>'


SELECT 
  ref.value('FilterID[1]', 'int') AS FilterID ,
  ref.value('Name[1]', 'NVARCHAR (10)') AS Name ,
  ref.value('Code[1]', 'NVARCHAR (10)') AS Code ,
  ref.value('Department[1]', 'NVARCHAR (3)') AS Department,
  ref.value('Number[1]', 'int') AS Number      
FROM @xmlData.nodes('/ArrayOfSpangemansFilter/SpangemansFilter') 
xmlData( ref )

产地:

FilterID    Name       Code       Department Number
----------- ---------- ---------- ---------- -----------
1219        Fred       510        N          305327
3578        Gary       001        B          692690
3579        George     001        X          35933

注意:需要[1]来表示您要选择序列的第一个值,因为查询可能每行返回多个匹配值(想象您的XML包含每个SpangemansFilter的多个FilterID)。 / p>

我认为这很有用,所以我用Google搜索并阅读了很多帖子,直到找到this one.

<强>更新 从文件加载:

DECLARE @xmlData XML
SET @xmlData = (
  SELECT * FROM OPENROWSET (
    BULK 'C:\yourfile.xml', SINGLE_CLOB
  ) AS xmlData
)

SELECT @xmlData

答案 1 :(得分:0)

就我而言 - 我感兴趣的数据包含在节点属性而不是值中。下面是一个如何访问属性的示例。

DECLARE @xmlData XML
set @xmlData='<?xml version="1.0"?>
<ArrayOfSpangemansFilter xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<SpangemansFilter FilterID="1219" Name="Fred" Code="510" Department="N" Number="305327">
</SpangemansFilter>
<SpangemansFilter FilterID="3578" Name="Gary" Code="001" Department="B" Number="0692690">
</SpangemansFilter>
<SpangemansFilter FilterID="3579" Name="George" Code="001" Department="X" Number="35933">
</SpangemansFilter>
</ArrayOfSpangemansFilter>'


SELECT 
  ref.value('@FilterID', 'int') AS FilterID ,
  ref.value('@Name', 'NVARCHAR (10)') AS Name ,
  ref.value('@Code', 'NVARCHAR (10)') AS Code ,
  ref.value('@Department', 'NVARCHAR (3)') AS Department,
  ref.value('@Number', 'int') AS Number      
FROM @xmlData.nodes('/ArrayOfSpangemansFilter/SpangemansFilter') 
xmlData( ref )