Using SSIS to extract a XML representation of table data to a file

前端 未结 2 939
小鲜肉
小鲜肉 2020-12-02 01:30

I\'m trying to use SSIS to extract XML representation of a query result set to a text file. My query is currently successfully extracting the exact XML output I need when I

相关标签:
2条回答
  • 2020-12-02 01:46

    Sorry to spoil, but there's an SSIS option for you: Export Column Transformation.

    I defined an OLE DB query with

    SELECT
        *
    FROM
    (
        SELECT * FROM dbo.spt_values FOR XML AUTO, ROOT('RootVal')
    ) D (xml_node)
    CROSS APPLY
    (
        SELECT 'C:\ssisdata\so_xmlExtract.xml'
    ) F (fileName)
    

    This results in 1 row and 2 columns in the dataflow. I then attached the Export Column Transformation and wired it up with xml_node as Extract Column and fileName as the File Path Column

    Mostly truncated results follow

    <RootVal>
        <dbo.spt_values name="rpc" number="1" type="A  " status="0"/>
        <dbo.spt_values name="dist" number="8" type="A  " status="0"/>
        <dbo.spt_values name="deferred" number="8192" type="V  " low="0" high="1" status="0"/>
    </RootVal>
    

    A more detailed answer, with pictures, is available on this Q&A Export Varbinary(max) column with ssis

    0 讨论(0)
  • 2020-12-02 01:53

    BillInKC's answer is the best I've ever seen, but SQL can be simplified (no need for cross apply):

    SELECT X.*, 'output.xml' AS filename
    FROM (SELECT * FROM #t FOR XML PATH('item'), ROOT('itemList')) AS X (xml_node)
    

    It will output the same structure:

    xml_node                                           filename
    -------------------------------------------------- ----------
    <itemList><item><num>1000</num></item></itemlist>  output.xml
    
    (1 row(s) affected)
    
    0 讨论(0)
提交回复
热议问题