how to export data to a XML file

Posted on 2006-04-25
Last Modified: 2008-03-03
is there any way for me to export the entire data in a table, i.e. select * from table; to be a XML file?

Question by:kennysflau
    LVL 75

    Expert Comment

    by:Aneesh Retnakaran
    select * from table for xml auto
    LVL 10

    Expert Comment

    or Use a DTS package
    LVL 75

    Expert Comment

    by:Aneesh Retnakaran
    if you are using windows authentication try this
    Exec master.dbo.xp_cmdshell 'bcp "Select * From urdb.dbo.urTable for xml auto" queryout c:\test.xml -c -S SIDEVDESKTOP  -T -E '

    if u r using sql server authentication use this
    exec master.dbo.xp_cmdshell 'bcp "Select k From urdb.dbo.urTable for xml auto" queryout c:\test.xml -c -S SIDEVDESKTOP -T -U sa -P '

    Author Comment

    Sorry could you please further elaborate a bit?
    LVL 75

    Accepted Solution

    select * from table for xml auto   -- will give the xml output in the Queryanalyzer result pane

    if you need to generate the output as a file, you need to use my second post  

    Author Comment

    can I control the structure, path and the name of the output XML file?
    LVL 75

    Expert Comment

    by:Aneesh Retnakaran
    Yes, you can do it ...
    Visit for examples
    LVL 4

    Assisted Solution

    Any table that has this basic structure can be exported:

    One record per row
    One column contains the record identifer
    The name of the respective image file is in a column or can be derived from a column
    The first step is to create a SELECT statement creating output that can be used with the FOR XML clause. SQL Server's syntax for doing this is somewhat complicated, but since we need a fairly simple XML file to import in MDID2, this example should be easy to adapt for your database.

    In Query Analyzer, enter the following statement (for brevity, we'll only export the first three fields of each record):


    1 AS Tag,

    NULL AS Parent,

    'ImageID' AS [data!1!identifier],

    NULL AS [record!2!resource],

    NULL AS [record!2!ImageID!element]

    NULL AS [record!2!CreatorName!element],

    NULL AS [record!2!CreationYear!element],



    2 AS Tag,

    1 AS Parent,

    'ImageID' AS [data!1!identifier],

    ImageID+'.jpg' AS [record!2!resource],

    ImageID AS [record!2!ImageID!element]

    CreatorName AS [record!2!CreatorName!element],

    CreationYear AS [record!2!CreationYear!element],

    FROM MDID..ResolvedCatalog

    If you included all fields from your table, the statement will be rather long. In order to continue the export process, we must create a view that allows us to reference this output with a simple name:

    CREATE VIEW DataExport


    1 AS Tag,

    NULL AS Parent,

    [statement created above continues here]

    Again, replace the name DataExport with a name meaningful for your case. After running the CREATE VIEW statement, the following SELECT statement should result in the same output as the original statement:

    SELECT * FROM DataExport

    To make sure the output is correctly formatted, run the following statement - if you receive any error messages, go back and make sure everything is set up correctly:


    At this point we are ready to export the data to an XML file. Open a command prompt and run SQL Server's bcp tool:

    bcp "SELECT * FROM MDID..DataExport FOR XML EXPLICIT" queryout data.xml -w -r "" -S sqlserver -T

    You will have to adapt the following parameters:

    The name of the database and the view you created (MDID..DataExport)
    The name of the XML output file (data.xml), pick a name meaningful for your case.
    The name of your SQL Server (sqlserver)
    The -T parameter causes bcp to use a trusted connection to your server, if you have to log in with a username and password, use the -U username -P password parameters instead.
    The resulting data.xml file is now ready for import into MDID2.

    Chetan Sachdeva

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    PRTG Network Monitor: Intuitive Network Monitoring

    Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

    When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
    This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
    Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
    Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now