Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
803 views
in Technique[技术] by (71.8m points)

sql server - Deserialize XML object in T-SQL

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:
enter image description here

Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...