如何从MSSQL

时间:2015-08-18 11:58:09

标签: sql-server xml

我有一个来自sql查询的字段中的XML,但是我想从中提取出EventID。

<Form>
  <Row LegID="1" EventID="8242323" MarType="WIN" DivType="FIXED" FMid="26881933" oID="4" />
  <Row LegID="2" EventID="8274278" MarType="WIN" DivType="FIXED" FMid="27000091" oID="1" />
  <Row LegID="3" EventID="8274189" MarType="HCWEST" DivType="FIXED" FMid="26999824" oID="2" />
  <Row LegID="4" EventID="8243036" MarType="WIN" DivType="FIXED" FMid="26884162" oID="2" />
  <Row LegID="5" EventID="8246510" MarType="WIN" DivType="FIXED" FMid="26898727" oID="2" />
  <Row LegID="6" EventID="8301802" MarType="WIN" DivType="FIXED" FMid="27101445" oID="4" />
  <Row LegID="7" EventID="8305000" MarType="WIN" DivType="FIXED" FMid="27111681" oID="1" />
  <Row LegID="8" EventID="8299986" MarType="WIN" DivType="FIXED" FMid="27095255" oID="1" />
</Form>

我尝试使用this page中的“Shredding XML”部分,但我猜这不起作用,因为我的XML不是用每个项目的打开和关闭标记形成的。

任何人都知道我正在尝试做的任务是什么?

// // EDIT

很棒,我可以阅读,但我有一个跟随问题。 我的选择是这样的:

SELECT LegNumber, X.Col.value('(Row/@EventID)[1]', 'varchar(25)') AS 'EventID'
FROM myTable mt
CROSS APPLY mt.MultiLegXML.nodes('/Form') as X(Col)

LegNumber是一个对应于XML中LegID的int。 If LegNumber = 2,如何从第二行读取EventID? (Where LegID = 2)

3 个答案:

答案 0 :(得分:1)

我希望这会对你有所帮助

    declare @xml xml;
    set @xml = '<Form>
      <Row LegID="1" EventID="8242323" MarType="WIN" DivType="FIXED" FMid="26881933" oID="4" />
      <Row LegID="2" EventID="8274278" MarType="WIN" DivType="FIXED" FMid="27000091" oID="1" />
      <Row LegID="3" EventID="8274189" MarType="HCWEST" DivType="FIXED" FMid="26999824" oID="2" />
      <Row LegID="4" EventID="8243036" MarType="WIN" DivType="FIXED" FMid="26884162" oID="2" />
      <Row LegID="5" EventID="8246510" MarType="WIN" DivType="FIXED" FMid="26898727" oID="2" />
      <Row LegID="6" EventID="8301802" MarType="WIN" DivType="FIXED" FMid="27101445" oID="4" />
      <Row LegID="7" EventID="8305000" MarType="WIN" DivType="FIXED" FMid="27111681" oID="1" />
      <Row LegID="8" EventID="8299986" MarType="WIN" DivType="FIXED" FMid="27095255" oID="1" />
    </Form>';


    select 
          C.value('@EventID', 'bigint') col
    from
         @xml.nodes('Form/Row') as X(C)
更新问题后

 select 
          C.value('@EventID', 'bigint') col
    from
         @xml.nodes('Form/Row') as X(C)
         where C.value('@LegID', 'bigint') = 2

答案 1 :(得分:1)

如果您只想从表中的每一行的XML列返回一个值,则不需要粉碎。使用sql:column()函数访问XQuery表格列中的值:

SELECT 
    LegNumber, 
    MultiLegXML.value('(/Form/Row[@LegID=sql:column("LegNumber")]/@EventID)[1]', 'varchar(25)') AS 'EventID'
FROM myTable mt

<强> Sqlfiddle Demo

输出

| LegNumber | EventID |
|-----------|---------|
|         2 | 8274278 |

如果要从一个XML数据返回多个EventID值,则需要进行粉碎,例如:

SELECT 
    LegNumber, 
    X.Col.value('@EventID', 'varchar(25)') AS 'EventID'
FROM myTable mt
CROSS APPLY mt.MultiLegXML.nodes('/Form/Row') as X(Col)

答案 2 :(得分:0)

好的绊倒我的方式。 : - )

SELECT *, X.Col.value('(Row/@EventID)[1]', 'varchar(25)') AS 'EventID'
FROM myTable mt
CROSS APPLY mt.MultiLegXML.nodes('/Form') as X(Col)