Deserialize XML object in T-SQL

自作多情 提交于 2020-01-10 02:05:50

问题


I've got an XML object. And I want to deserialize it into a table using T-SQL.

<Params>
    <type = 1> 
        <value> 10 </value>
    </type>

    <type = 2> 
        <value> abc </value>
    </type>
</Params>

How can I store this data into a table like this:

Thanks!


回答1:


Your XML is not valid - but if you had something like this:

<Params>
    <type ID="1"> 
        <value> 10 </value>
    </type>
    <type ID="2"> 
        <value> abc </value>
    </type>
</Params>

then you could use this XQuery / SQL statement to get what you're looking for:

DECLARE @XML XML = '<Params>
    <type ID="1"> 
        <value> 10 </value>
    </type>
    <type ID="2"> 
        <value> abc </value>
    </type>
</Params>'

SELECT
    Type = TypeNode.value('@ID', 'int'),
    NodeValue = TypeNode.value('(value)[1]', 'varchar(50)')
FROM
    @XML.nodes('/Params/type') AS XTbl(TypeNode)

I'm not clear how/what the id column is supposed to be - care to explain?



来源:https://stackoverflow.com/questions/13562635/deserialize-xml-object-in-t-sql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!