Parsing nested XML into SQL table

前端 未结 3 1953
南笙
南笙 2020-12-29 17:22

What would be the right way to parse the following XML block into SQL Server table according to desired layout (below)? Is it possible to do it with a single SELECT statemen

相关标签:
3条回答
  • 2020-12-29 18:16

    You can make use of a numbers table to pick the first, second, third etc row from the child elements. In this query I have limited the rows returned to the number if dates provided. If there are more values or descriptions than dates you have to modify the join to take that into account.

    declare @XML xml = '
    <ObjectData>
      <Parameter1>some value</Parameter1>
      <Parameter2>other value</Parameter2>
      <Dates>
        <dateTime>2011-02-01T00:00:00</dateTime>
        <dateTime>2011-03-01T00:00:00</dateTime>
        <dateTime>2011-04-01T00:00:00</dateTime>
      </Dates>
      <Values>
        <double>0.019974</double>
        <double>0.005395</double>
        <double>0.004854</double>
      </Values>
      <Description>
        <string>this is row 1</string>
        <string>this is row 2</string>
        <string>this is row 3</string>
      </Description>
    </ObjectData>'
    
    ;with Numbers as
    (
      select number
      from master..spt_values
      where type = 'P'
    )
    select T.N.value('Parameter1[1]', 'varchar(50)') as Parameter1,
           T.N.value('Parameter2[1]', 'varchar(50)') as Parameter2,
           T.N.value('(Dates/dateTime[position()=sql:column("N.Number")])[1]', 'datetime') as Dates,
           T.N.value('(Values/double[position()=sql:column("N.Number")])[1]', 'float') as [Values],
           T.N.value('(Description/string[position()=sql:column("N.Number")])[1]', 'varchar(max)') as [Description]
    from @XML.nodes('/ObjectData') as T(N)
      cross join Numbers as N
    where N.number between 1 and (T.N.value('count(Dates/dateTime)', 'int'))
    
    0 讨论(0)
  • 2020-12-29 18:22

    Use the OPENXML function. It is a rowset provider (it returns the set of rows parsed from the XML) and thus can be utilized in SELECT or INSERT like:

    INSERT INTO table SELECT * FROM OPENXML(source, rowpattern, flags)
    

    Please see the first example in the documentation link for clarity.

    0 讨论(0)
  • 2020-12-29 18:29

    Typically, if you wanted to parse XML, you'd do it a programming language like Perl, Python, Java or C# that a) has an XML DOM, and b) can communicate with a relational database.

    Here's a short article that shows you some of the basics of reading and writing XML in C# ... and even has an example of how to create an XML document from a SQL query (in one line!):

    http://www.c-sharpcorner.com/uploadfile/mahesh/readwritexmltutmellli2111282005041517am/readwritexmltutmellli21.aspx

    0 讨论(0)
提交回复
热议问题