The Best Way to shred XML data into SQL Server database columns

后端 未结 8 1734
萌比男神i
萌比男神i 2020-11-28 02:58

What is the best way to shred XML data into various database columns? So far I have mainly been using the nodes and value functions like so:

INSERT INTO some         


        
8条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-28 03:36

    in my case i'm running SQL 2005 SP2 (9.0).

    The only thing that helped was adding OPTION ( OPTIMIZE FOR ( @your_xml_var = NULL ) ). Explanation is on the link below.

    Example:

    INSERT INTO @tbl (Tbl_ID, Name, Value, ParamData)
    SELECT     1,
        tbl.cols.value('name[1]', 'nvarchar(255)'),
        tbl.cols.value('value[1]', 'nvarchar(255)'),
        tbl.cols.query('./paramdata[1]')
    FROM @xml.nodes('//root') as tbl(cols) OPTION ( OPTIMIZE FOR ( @xml = NULL ) )
    

    https://connect.microsoft.com/SQLServer/feedback/details/562092/an-insert-statement-using-xml-nodes-is-very-very-very-slow-in-sql2008-sp1

提交回复
热议问题