how to export data to a XML file

is there any way for me to export the entire data in a table, i.e. select * from table; to be a XML file?

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Aneesh RetnakaranDatabase AdministratorCommented:
select * from table for xml auto
or Use a DTS package
Aneesh RetnakaranDatabase AdministratorCommented:
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 '
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

kennysflauAuthor Commented:
Sorry could you please further elaborate a bit?
Aneesh RetnakaranDatabase AdministratorCommented:
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  

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kennysflauAuthor Commented:
can I control the structure, path and the name of the output XML file?
Aneesh RetnakaranDatabase AdministratorCommented:
Yes, you can do it ...
Visit for examples
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:



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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.