?
Solved

how to export data to a XML file

Posted on 2006-04-25
8
Medium Priority
?
307 Views
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?

Thanks!!!
0
Comment
Question by:kennysflau
8 Comments
 
LVL 75

Expert Comment

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

Expert Comment

by:StephenCairns
ID: 16541029
or Use a DTS package
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 16541058
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 '
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:kennysflau
ID: 16541071
Sorry could you please further elaborate a bit?
0
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 300 total points
ID: 16541146
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  
0
 

Author Comment

by:kennysflau
ID: 16541409
can I control the structure, path and the name of the output XML file?
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 16541502
Yes, you can do it ...
Visit http://www.sqlxml.org for examples
0
 
LVL 4

Assisted Solution

by:csachdeva
csachdeva earned 200 total points
ID: 16542877
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):

SELECT

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],

UNION ALL

SELECT

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

SELECT

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:

SELECT * FROM DataExport FOR XML EXPLICIT

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.

Regards,
Chetan Sachdeva
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Suggested Courses

850 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