Saturday, July 31, 2010
Home Retrieving XML Writing XML General Contact About  

Is there a way to export XML from SQL using a stored procedure? (94391 Requests)

In addition to the information below, take a look at this FAQ at SQLTEAM.com. The solution shown at SQLTEAM.com does not have a size limitation which some people have reported with the solution shown below.

You first generate a file with the text-editor like:


<root>
<%begindetail%>
<%insert_data_here%>
<%enddetail%>
</root>

Save it as c:\temp\template.tpl

Next you open QA and type:


sp_makewebtask @outputfile = 'c:\temp\myxmlfile.xml', 
	@query = 'select * from sysobjects for xml auto',  
	@templatefile = 'c:\temp\template.tpl'

The Result is a XML-File!

Answer provided by Bernhard Pichler.


Feedback

# Is there a way to export XML from SQL using a stored procedure? Jeb Bushell

If you don't have SA privilege you are SOL because sp_MakeWebTask explicitly checks for it.

# Another Way Rob

If you have trouble making this work (excellent solution, BTW!), you can also try the method described here: http://www.sqlteam.com/Forums/topic.asp?TOPIC_ID=9336&FORUM_ID=5&CAT_ID=3&Topic_Title=SQL+Server+2000+XML&Forum_Title=Developer

# help sahu

while executing this query it gives error message that "Only members of the sysadmin role can execute this stored procedure. " Please let me know if there is some other method to execute this method

# Root Node Jacob

With this method we are forced to specify a Node in the template file. What if i am generating the complete XML structure from the stored procedure and i don't want to add additional tag like the tag in this case ?

# Working with larger data sets Chris L

This only works for a small data set. How about a large data set? sp_makewebtask is adding 2 CRLF character combinations at the end of each row (if you run the query in QA). Am I missing something? Here is the error where it is the beginning of the first line after the first row: Missing equals sign between attribute and attribute value. Error processing resource 'file:///C:/myxmlfile.xml'. Line 5, Position 1 nfo="0" parent_obj="0" crdate="2000-08-06T01:29:12.717" ftcatid="0" schema_ver="0" stats_schema_ver="0" type="S " userstat="1" sysstat="65" indexdel="0" refdate="2000-08-06T01:29:12.717" version="0" deltrig="0" instrig="0" updtrig="0" seltrig="0" category="2" cache="0"/>Working with larger data sets Jim B

I have the same problem. The extra CR/LF are added every 2033 characters.

# Working with larger data sets Johnny B

Same problem here. Otherwise it seams to be a great way of retrieving XML data from SQL Server. Running a Query in QA like: "SELECT * FROM FOR XML AUTO, ELEMENTS" returns a row for each amount of 2033 characters and therefore per row 2 extra CRLF's. How to handle this?

# Article Jeff G

There is a nice article on exporting SQL Server Data at http://www.PerfectXML.com/Articles/XML/ExportSQLXML.asp Check it out!!!

# Working with larger data sets jay w

same problem for me. Found microsoft article here: http://support.microsoft.com/default.aspx?scid=KB;en-us;q275583 Their hack workaround suggestions are a pain. I ended up just building xml tags in the select statement and totally bypassed the SQL FOR XML clause. For example Select '' FROM table. I then exported the data to a .xml file with the method listed above.

# Error 16805 Mike

Should be exactly what I want but get error 16805 'SQL Web Assistant: Could not execute the SQL statement.' even though I have sa permission and the query is valid.

# Error 16805 Nitin Gupta

Mike, I am having the same problem. I used the Web Assistant Wizard and I get the same problem. Let me know if you get a work around. Thanks, Nitin niting@digitalinfuzion.com

# Error 16805 Ruchi

I am also having the same problem. Can anybody help me. My email id is ruchinarang29@yahoo.co.in. Thanks,Ruchi

# Working with larger data sets Bryan Avery

Try using DBCC TRACEON(257) Works a treat

# Error 16805 Kossol

I had this problem when having the webtask run a sproc which itself executed dynamic sql (build a string called @SQL the 'exec (@sql)). I tweaked the sql it was running a bit and got it working but I have no idea why some code works but not others. Hope this gives someone a place to start looking to troubleshoot..

# Error 16805 Pam

I am having the same exact problem! I know that it has to be permissions related, another user with supposedly the same permissions is able to run the task and I am not. If anyone has any insight, please let me know. Thanks! Pam pjlahoud@yahoo.com

# Admin writes to sp_makewebtask Bryan Avery

This function is ok but you need to have admin writes to run sp_makewebtask, is there any other method, my user is IUSR_?????? which has only got execute write

# Admin writes to sp_makewebtask Stuart Roberts

Is there a way to output the data to the xml file but instead of overwriting it append the new data to the end?

# Append File Stuart Roberts

Is there a way to output the data to the xml file but instead of overwriting it append the new data to the end?

# Root Node Drew

You can remove the <root> tag from the template file, and only have <%begindetail%> <%insert_data_here%> <%enddetail%> in your template file. Works fine!

# Error 16805 JRuiz

I had the same problem, because I was used FOR XML RAW, ELEMTS. But if you use FOR XML RAW the problem disapear. Sorry, about my poor english.

# re: Is there a way to export XML from SQL using a stored procedure? himanshu

it shows me following error..please help..

ODBC: Msg 0, Level 16, State 1
Cannot load the DLL xpweb70.dll, or one of the DLLs it references. Reason: 126(The specified module could not be found.).

# re: Is there a way to export XML from SQL using a stored procedure? Bryant

Are you using SQL Server 2000?

# re: Is there a way to export XML from SQL using a stored procedure? Mark h

It's OK for a simple XML doc- but who uses a simple XML document? I need it for an XML document where the nodes are not equally nested ie they're not a simple table/field name structure.. :-(

# re: Is there a way to export XML from SQL using a stored procedure? TJ

I am getting
"SQL Web Assistant: Could not open the template file" error the file template file is on my c drive and I am trying to run

sp_makewebtask @outputfile = 'c:\temp\myxmlfile.xml',
@query = 'select * from sysobjects for xml auto',
@templatefile = 'c:\temp\template.tpl'

# re: Is there a way to export XML from SQL using a stored procedure? TR

Regarding the "SQL Web Assistant: Could not open the template file" error:

I think that you created the template file on your local computer and that you have a database server somewhere else. The specified path is on the database server. You probably have to create the template file on the server.

# re: Is there a way to export XML from SQL using a stored procedure? Abhay

Getting the error:Server: 16805
SQL Web Assistant: Could not execute the SQL statement. Any Idea, Help. Thanks Abhay

# re: Is there a way to export XML from SQL using a stored procedure? Abhay

Getting the error:Server: 16805
SQL Web Assistant: Could not execute the SQL statement. Any Idea, Help. Thanks Abhay

# re: Is there a way to export XML from SQL using a stored procedure? Bryant

Does your sql statement run in query analyzer?

# Dreaded 16805 error Todd

I am getting the dreaded error. I am running everything locally
EXEC sp_makewebtask
@outputfile = 'c:\temp\Shippers.xml',
@query = 'SELECT * FROM macl_Directory FOR XML AUTO',
@templatefile ='c:\temp\template.txt'

and still getting the error. I am an admin on the box as well as SQL

help????

# re: Is there a way to export XML from SQL using a stored procedure? sanghamitro Ghosh

u are specifying template file as .TXT
but it should be .tpl

# re: Is there a way to export XML from SQL using a stored procedure? Nat

I'm a bit of a newbie to this, so bear with me...

I need to have a saved stored proc on the server ready to be run via a browser(i know how to that part). The SP will need export a single SQL table to a file so that it can be emailed through to another person and imported into a local database. an xml file would be ideal. Im struggling with coding the stored procedure, specifically that part that will deal with creating the exported file. Can anyone help?

# re: Is there a way to export XML from SQL using a stored procedure? Sanghamitro Ghosh

Please create an web task and execute in sql server to generate the required xml format. Place the output xml format in the location required.
The location of the XML out put file shld be specified in the Web task script. Its simple
try out :)

# re: Is there a way to export XML from SQL using a stored procedure? Erik McCarty erik@texastar.com

use pubs

create table root ( data char(1) NULL )

execute master..xp_cmdshell 'bcp "select top 10 * from pubs..root right outer join pubs..authors on 1 = 1 for xml auto" queryout \\stagingsql\Prod_bak\CC7335\bcp.xml -T -c -r -t'


<pubs..root>
<pubs..authors au_id="172-32-1176" au_lname="White" au_fname="Johnson" phone="408 496-7223" address="10932 Bigge Rd." city="Menlo Park" state="CA" zip="94025" contract="1"/>
<pubs..authors au_id="213-46-8915" au_lname="Green" au_fname="Marjorie" phone="415 986-7020" address="309 63rd St. #411" city="Oakland" state="CA" zip="94618" contract="1"/>
<pubs..authors au_id="238-95-7766" au_lname="Carson" au_fname="Cheryl" phone="415 548-7723" address="589 Darwin Ln." city="Berkeley" state="CA" zip="94705" contract="1"/>
<pubs..authors au_id="267-41-2394" au_lname="O&apos;Leary" au_fname="Michael" phone="408 286-2428" address="22 Cleveland Av. #14" city="San Jose" state="CA" zip="95128" contract="1"/>
<pubs..authors au_id="274-80-9391" au_lname="Straight" au_fname="Dean" phone="415 834-2919" address="5420 College Av." city="Oakland" state="CA" zip="94609" contract="1"/>
<pubs..authors au_id="341-22-1782" au_lname="Smith" au_fname="Meander" phone="913 843-0462" address="10 Mississippi Dr." city="Lawrence" state="KS" zip="66044" contract="0"/>
<pubs..authors au_id="409-56-7008"

# To all the have error on execution Greg Grater

I had the same problem with my template file. First, make sure the template file ends in .tpl (suggested above). Second, make sure that the account SQL Server runs under has read\write access to that file. Remember, you're executing the stored procedure from SQL server which runs under a different account than you do personally.

# re: Is there a way to export XML from SQL using a stored procedure? Colin Sheppard

Has there been any resolution to this problem?:

ODBC: Msg 0, Level 16, State 1
Cannot load the DLL xpweb70.dll, or one of the DLLs it references. Reason: 126(The specified module could not be found.).

# re: Is there a way to export XML from SQL using a stored procedure? Sebastián Rivero

Can I make with that tool something like the following sample?:

<root>
<group titulo="group1">
<sintesis>XXXX</sintesis>
<notes>
<note>Note1</note>
<note>Note2</note>
<note>Note3</note>
</notes>
</group>
<group titulo="group2">
<sintesis>YYYY</sintesis>
<notes>
<note>Note1</note>
<note>Note2</note>
<note>Note3</note>
</notes>
</group>
</root>

# re: Is there a way to export XML from SQL using a stored procedure? Sebastián Rivero

Can I make with that tool something like the following sample?:

<root>
<group titulo="group1">
<sintesis>XXXX</sintesis>
<notes>
<note>Note1</note>
<note>Note2</note>
<note>Note3</note>
</notes>
</group>
<group titulo="group2">
<sintesis>YYYY</sintesis>
<notes>
<note>Note1</note>
<note>Note2</note>
<note>Note3</note>
</notes>
</group>
</root>

# re: Is there a way to export XML from SQL using a stored procedure? Meher

Is there any function that could filter the invalid characterfrom a text when it generates an XML file?
I get a message in IE when it tried to parse the XML generated:
"A name contained an invalid character"

# re: Working with larger data sets Soumen

If i use DBCC TRACEON(257) just before the SELECT statement, i still get line breaks at line number 87, then 174 (which is twice of 87), etc. So DBCC TRACEON(257) is not working completely well.

# re: Is there a way to export XML from SQL using a stored procedure? Ken

This snippet of code is a suggestion on exporting the XML data, then
sending that data via email.
Basically, it uses BCP (BulkCopy program) to execute the stored procedure
that creates the XML data, creates a unique file name to export the data to,
then sends the XML data file as an attachment via email.
I got this idea from http://www.sqlteam.com/item.asp?ItemID=4722

DECLARE @CurrentDate AS datetime
DECLARE @YYYYMMDD AS varchar(10)
DECLARE @HHMMSS AS varchar(12)
DECLARE @BasePath AS varchar(255)
DECLARE @FileName AS varchar(500)
DECLARE @bcpCommand AS varchar(1000)
DECLARE @spParm AS varchar(10)
DECLARE @subjectText AS varchar(255)
DECLARE @CleanUpFile AS varchar(555)

-- This is a parm to the stp_SOXMLAccountProfile stored procedure.
-- stp_SOXMLAccountProfile is a stored procedure that creates the well-formed
-- XML document. If anyone would like to
-- see that code, I'll check this posting
-- often to see if it's requested.

-- This would either be passed in as a parameter, or retrieved
-- from a data table
SET @spParm = '999999'

-- Create the date and time portion of the file name we're going
-- to export to.
SET @CurrentDate = GETDATE()
SET @YYYYMMDD = REPLACE(CONVERT(char(10), @CurrentDate, 111), '/', '-')
SET @HHMMSS = REPLACE( CONVERT(char(12), @CurrentDate, 114) ,':', '-')

-- Construct the unique filename we're going to export to.
SET @BasePath = (SELECT DataValue FROM ControlTable
WHERE Module = 'so_xml_accountprofile'
AND Variable = 'basepath')
SET @FileName = @BasePath + 'SO_XML_AP_' + @spParm + '_' +
@YYYYMMDD + '-' + @HHMMSS + '.xml'

-- Create the bulkcopy command we want to execute
SET @bcpCommand = 'bcp "EXEC stp_SOXMLAccountProfile ' + CHAR(39) + @spPar

# re: Is there a way to export XML from SQL using a stored procedure? Ken

Part 2 of above code
(Cutoff by submit process)

-- Create the bulkcopy command we want to execute
SET @bcpCommand = 'bcp "EXEC stp_SOXMLAccountProfile ' + CHAR(39) + @spParm +
CHAR(39) + '" queryout "' + @FileName + '" -S ' + @@SERVERNAME +
' -U youruserid -P yourpswd -f ' + CHAR(34) + @BasePath +
'bcp_SOXMLAccountProfile.fmt' + CHAR(34)

-- Now execute the bulkcopy command
EXEC master..xp_cmdshell @bcpCommand

-- Email stuff
SET @subjectText = 'SalesOutlook Acount Profile Update for ' + @spParm

EXEC master..xp_sendmail @recipients = 'yourname@domain.com',
@message = 'Here is the Sales Outlook data file you requested.',
@attachments = @FileName,
@subject = @subjectText


SET @CleanUpFile = 'DEL ' + @FileName
EXEC master..xp_cmdshell @CleanUpFile

# re: Is there a way to export XML from SQL using a stored procedure? Ken

Part 2 of above code
(Cutoff by submit process)

-- Create the bulkcopy command we want to execute
SET @bcpCommand = 'bcp "EXEC stp_SOXMLAccountProfile ' + CHAR(39) + @spParm +
CHAR(39) + '" queryout "' + @FileName + '" -S ' + @@SERVERNAME +
' -U youruserid -P yourpswd -f ' + CHAR(34) + @BasePath +
'bcp_SOXMLAccountProfile.fmt' + CHAR(34)

-- Now execute the bulkcopy command
EXEC master..xp_cmdshell @bcpCommand

-- Email stuff
SET @subjectText = 'SalesOutlook Acount Profile Update for ' + @spParm

EXEC master..xp_sendmail @recipients = 'yourname@domain.com',
@message = 'Here is the Sales Outlook data file you requested.',
@attachments = @FileName,
@subject = @subjectText


SET @CleanUpFile = 'DEL ' + @FileName
EXEC master..xp_cmdshell @CleanUpFile

# re: Is there a way to export XML from SQL using a stored procedure? Russ

Is there a way to call a stored procedure via HTTP?

i.e. http://lameserver/Northwind/sp_my_stored_procedure&param1=value1&@param2=value2

Is there something like the above?

# re: Is there a way to export XML from SQL using a stored procedure? mathkids

Yes. Look up the docs on xpath within s2000k there's quite a bit there.

# re: Is there a way to export XML from SQL using a stored procedure? David

Hi Ken:

Would you like to show me the code for stp_SOXMLAccountProfile stored procedure, please?

Thanks

David

# re: Is there a way to export XML from SQL using a stored procedure? David

Hi Ken:

Would you like to show me the code for stp_SOXMLAccountProfile stored procedure, please?

Thanks

David

# re: Is there a way to export XML from SQL using a stored procedure? vipin mukesh

if i execute this qurey

EXEC sp_makewebtask
@outputfile = '\\DEMOSERVER\ShippersXML.xml',
@query = 'SELECT * FROM Shippers FOR XML AUTO',
@templatefile ='\\DEMOSERVER\temp\Shippers.tpl'

then this message has been shown :-

Server: Msg 16821, Level 11, State 1, Procedure sp_makewebtask, Line 125
SQL Web Assistant: Could not open the output file.

demoserver is the server where my sql server run
what should i do plzz help me

# stp_SOXMLAccountProfile achen

Hi Ken:

may I see your stp_SOXMLAccountProfile code ?
thanks !
amy

# Any way to export XML from SQL using the stored procedure sp_makewebtask? sandee

Is there a way to export XML from SQL using a stored procedure sp_MakeWebTask with something like the following sample?:

<root>
<group titulo="group1">
<sintesis>XXXX</sintesis>
<notes>
<note>Note1</note>
<note>Note2</note>
<note>Note3</note>
</notes>
</group>
<group titulo="group2">
<sintesis>YYYY</sintesis>
<notes>
<note>Note1</note>
<note>Note2</note>
<note>Note3</note>
</notes>
</group>
</root>

# Is there a way save an outfile in sp_makewebtask into a different server Mark

if i execute this query i get an error:
SQL Web Assistant: Could not open the output file.

EXEC sp_makewebtask
@outputfile = '\\192.168.0.8\output.asp',
@query = 'select id, name from sample'
@templatefile ='\\192.168.0.1\sample.tpl'

can anyone help?

# re: Is there a way to export XML from SQL using a stored procedure? winson

Appreciate if any one can provide advice on this question by sandee. I'm looking for the same thing

Is there a way to export XML from SQL using a stored procedure sp_MakeWebTask with something like the following sample?:

<root>
<group titulo="group1">
<sintesis>XXXX</sintesis>
<notes>
<note>Note1</note>
<note>Note2</note>
<note>Note3</note>
</notes>
</group>
<group titulo="group2">
<sintesis>YYYY</sintesis>
<notes>
<note>Note1</note>
<note>Note2</note>
<note>Note3</note>
</notes>
</group>
</root>

# re: Is there a way to export XML from SQL using a stored procedure? Essa

Try running the same query as a job. Create a job of the following code.

EXEC sp_makewebtask
@outputfile = '\\192.168.0.8\output.asp',
@query = 'select id, name from sample'
@templatefile ='\\192.168.0.1\sample.tpl'

Thanks

# re: Is there a way to export XML from SQL using a stored procedure? Essa

Try running the same query as a job. Create a job of the following code.

EXEC sp_makewebtask
@outputfile = '\\192.168.0.8\output.asp',
@query = 'select id, name from sample'
@templatefile ='\\192.168.0.1\sample.tpl'

Thanks

# re: Is there a way to export XML from SQL using a stored procedure? Mike

I dont see the solution here. Anyone got the error resolved?

# re: Is there a way to export XML from SQL using a stored procedure? Ivars

How the tpl file should look like?

# re: Is there a way to export XML from SQL using a stored procedure? Joe Doakes

stp_SOXMLAccountProfile

Source code please as promised?

Doakes

# re: Is there a way to export XML from SQL using a stored procedure? r

erwrwe

# re: Is there a way to export XML from SQL using a stored procedure? Wobbly Dave

ODBC: Msg 0, Level 16, State 1 Cannot load the DLL xpweb70.dll

I found that this did work against the default instance, but not the named, quite simply because the xpweb70.dll didnt exist in the MSSQL\Binn directory for the named instances. To cure it I copied the dll out and put it in the Binn directory - the makewebtask procedure then ran just fine

# re: Is there a way to export XML from SQL using a stored procedure? hrubesh

hi have the same problem
Msg 16822, Level 11, State 1, Procedure sp_makewebtask, Line 131
SQL Web Assistant: Could not open the template file.

# re: Is there a way to export XML from SQL using a stored procedure? hrubesh

hi have the same problem
Msg 16822, Level 11, State 1, Procedure sp_makewebtask, Line 131
SQL Web Assistant: Could not open the template file.

# re: Is there a way to export XML from SQL using a stored procedure? jadra

I have a problem with line breaks that sp_MakeWebTask does every 2033 characters. I have to fix them manually everytime, which is pretty annoying.

Some guy on another forum wrote to use DTS, and connect to the database as the OLEDB. I did that, but it did not solve the problem.

If anyone has the solution, i'm all ears...

# re: Is there a way to export XML from SQL using a stored procedure? jadra

Oh, never mind... Silly me....Found the answer.

# Instead of overwriting i want to append the new data to the end Mani

Instead of overwriting i want to append the new data to the end ot the existing file.

please email your answers to mani_sai@yahoo.com

Thanks.

# Instead of overwriting i want to append the new data to the end Mani

Instead of overwriting i want to append the new data to the end ot the existing file.

please email your answers to mani_sai@yahoo.com

Thanks.

# Instead of overwriting i want to append the new data to the end Mani

Instead of overwriting i want to append the new data to the end ot the existing file.

please email your answers to mani_sai@yahoo.com

Thanks.

# re: Is there a way to export XML from SQL using a stored procedure? Khan

I am still getting the "SQL Web Assistant: Could not open the template file." error message. Did anyone figure out why? I just copied the code from the top of this page and tried to execute on a DB that does not have any named instance (in case if that matters).

I tried to put the entire execute command in one line - still no result.

Any help?

# re: Is there a way to export XML from SQL using a stored procedure? Jade S

"SQL Web Assistant: Could not open the template file."

I figured this out. In my case, the SQL Server is on a remote server, and the path the server sees was not valid. Example, I had the template in "C:\TEMP\TEMPLATE.txt", but this file did not exist on the server (it was on my local machine) I ended up sharing my local folder and having the server access the file through UNC names "\\machineName\shareName\TEMPLATE.txt"

# re: Is there a way to export XML from SQL using a stored procedure? Jade S

SQL Web Assistant: Could not open the output file.

Check folder permissions where the file is located. I set Everyone to have full access and that resolved my issue (not recommended, but proof of concept none the less) I can't say for sure what user needs permissions, but this is a start.

# bcp queryout with where clause Harsha

Hi,
I have a problem.
I want to export in csv format with where conditions in the query for an application. I cannot take global temporary table neithe temporary table. Please help

# re: Is there a way to export XML from SQL using a stored procedure? le thannh Diep

Toi la...

# re: Is there a way to export XML from SQL using a stored procedure? le thannh Diep

Toi la...

# sql to XML le thannh Diep

hom nay troi rat dep.

# Templatefile for 2005 Dominik

In SQL 2005 the sp_makewebtask ist depricated but I have to write some version information in the xml file?

I tried some sql tricks, but didn'nt work. I there a possibility to write in the file to append?

reg dominik

# re: Is there a way to export XML from SQL using a stored procedure? uppi

SQL Web Assistant: Could not open the output file.

Even i checked the file permissions and i created the template.tpl in server box(which having sql server2005) i got the above problem ....so if any one help me to solve my problem ..

# re: Is there a way to export XML from SQL using a stored procedure? uppi

SQL Web Assistant: Could not open the output file.

Even i checked the file permissions and i created the template.tpl in server box(which having sql server2005) i got the above problem ....so if any one help me to solve my problem ..

reply me to uppi551@yahoo.co.in

# YnshYyEbgUKLf JXL40

Atlantic Yards project, the largest ever in Brooklyn, to build a basketball arena plus at least 16 high-rise buildings. ,

# YnshYyEbgUKLf JXL40

Atlantic Yards project, the largest ever in Brooklyn, to build a basketball arena plus at least 16 high-rise buildings. ,

# SOLUTION re SQL Web Assistant: Could not open the output file. JD

Hi guys,
The input provided by Jade S gave me final solution: I createed a new folder and gave myself AND the server full access.

Then I executed the
EXEC sp_makewebtask (etc)

It created the file and incl result hoped for.
thx for input.

So many hours spend - and so simple.
br
JD

Title  
Name  
Url
Comments   


FAQ #29

last updated:
7/3/2002


Did the information in this faq help answer your question?





 
 

Survey Results: 106
Yes No N/A

© 2001, 2002, 2003 sqlxml.org