Chris McGuigan
asked on
Saving XML output automatically
I have a simple SQL query which generates output in XML. I need to automate this and save the output to a text file.
I tried using DTS but ended up with a file full of hexadecimal codes.
I've tried running sp_makewebtask as suggested elsewhere here but I hit security issues at every turn.
There must be an easy way to do this - but I can't find it!
TIA
Chris McGuigan
I tried using DTS but ended up with a file full of hexadecimal codes.
I've tried running sp_makewebtask as suggested elsewhere here but I hit security issues at every turn.
There must be an easy way to do this - but I can't find it!
TIA
Chris McGuigan
HERE IS a simple ASP Example
<%
Option Explicit
Response.ContentType = "text/xml"
Dim ObjADORS
Const adPersistXML = 1
Set ObjADORS = Server.CreateObject("ADODB .Recordset ")
ObjADORS.Open "SELECT ContactName FROM Customers", _
"PROVIDER=SQLOLEDB.1;SERVE R=.;UID=sa ;PWD=;DATA BASE=North wind;"
ObjADORS.Save Response, adPersistXML
ObjADORS.Close
Set ObjADORS = Nothing
%>
<%
Option Explicit
Response.ContentType = "text/xml"
Dim ObjADORS
Const adPersistXML = 1
Set ObjADORS = Server.CreateObject("ADODB
ObjADORS.Open "SELECT ContactName FROM Customers", _
"PROVIDER=SQLOLEDB.1;SERVE
ObjADORS.Save Response, adPersistXML
ObjADORS.Close
Set ObjADORS = Nothing
%>
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
sp_makewebtask is by far the easiest way to do this. If youve got permissions problems with this then regardless, you will end up with permissions problems on any other method.
ASKER
I've simplified everything, heres the query followed by the results when run in query analyser.
SELECT top 2 RTRIM(SOPNUMBE) 'InvoiceNo', RTRIM(ORIGNUMB) 'OrderNo', ACTLSHIP 'Shipped', RTRIM(CUSTNMBR) 'CustomerNo', RTRIM(CSTPONBR) 'CustomerPO'
FROM SOP30200 As DespatchNotes
WHERE (SOPTYPE = '3')
for xml auto, xmldata, elements
<Schema name="Schema9" xmlns="urn:schemas-microso ft-com:xml -data" xmlns:dt="urn:schemas-micr osoft-com: datatypes" >
<ElementType name="DespatchNotes" content="eltOnly" model="closed" order="many">
<element type="InvoiceNo"/>
<element type="OrderNo"/>
<element type="Shipped"/>
<element type="CustomerNo"/>
<element type="CustomerPO"/>
</ElementType>
<ElementType name="InvoiceNo" content="textOnly" model="closed" dt:type="string"/>
<ElementType name="OrderNo" content="textOnly" model="closed" dt:type="string"/>
<ElementType name="Shipped" content="textOnly" model="closed" dt:type="dateTime"/>
<ElementType name="CustomerNo" content="textOnly" model="closed" dt:type="string"/>
<ElementType name="CustomerPO" content="textOnly" model="closed" dt:type="string"/>
</Schema>
<DespatchNotes xmlns="x-schema:#Schema9">
<InvoiceNo>INV6000057</Inv oiceNo>
<OrderNo>ORD6000116</Order No>
<Shipped>2001-05-10T00:00: 00</Shippe d>
<CustomerNo>LAN001</Custom erNo>
<CustomerPO>PO044423</Cust omerPO>
</DespatchNotes>
<DespatchNotes xmlns="x-schema:#Schema9">
<InvoiceNo>INV6000058</Inv oiceNo>
<OrderNo>ORD6000122</Order No>
<Shipped>2001-05-10T00:00: 00</Shippe d>
<CustomerNo>PRO001</Custom erNo>
<CustomerPO>PCN0510/PF1</C ustomerPO>
</DespatchNotes>
This is some of what I get in the text file;
440949006E0076006F00690063 0065004E00 6F0044074F 0072006400 650072004E 006F004407 5300680069 0070007
SELECT top 2 RTRIM(SOPNUMBE) 'InvoiceNo', RTRIM(ORIGNUMB) 'OrderNo', ACTLSHIP 'Shipped', RTRIM(CUSTNMBR) 'CustomerNo', RTRIM(CSTPONBR) 'CustomerPO'
FROM SOP30200 As DespatchNotes
WHERE (SOPTYPE = '3')
for xml auto, xmldata, elements
<Schema name="Schema9" xmlns="urn:schemas-microso
<ElementType name="DespatchNotes" content="eltOnly" model="closed" order="many">
<element type="InvoiceNo"/>
<element type="OrderNo"/>
<element type="Shipped"/>
<element type="CustomerNo"/>
<element type="CustomerPO"/>
</ElementType>
<ElementType name="InvoiceNo" content="textOnly" model="closed" dt:type="string"/>
<ElementType name="OrderNo" content="textOnly" model="closed" dt:type="string"/>
<ElementType name="Shipped" content="textOnly" model="closed" dt:type="dateTime"/>
<ElementType name="CustomerNo" content="textOnly" model="closed" dt:type="string"/>
<ElementType name="CustomerPO" content="textOnly" model="closed" dt:type="string"/>
</Schema>
<DespatchNotes xmlns="x-schema:#Schema9">
<InvoiceNo>INV6000057</Inv
<OrderNo>ORD6000116</Order
<Shipped>2001-05-10T00:00:
<CustomerNo>LAN001</Custom
<CustomerPO>PO044423</Cust
</DespatchNotes>
<DespatchNotes xmlns="x-schema:#Schema9">
<InvoiceNo>INV6000058</Inv
<OrderNo>ORD6000122</Order
<Shipped>2001-05-10T00:00:
<CustomerNo>PRO001</Custom
<CustomerPO>PCN0510/PF1</C
</DespatchNotes>
This is some of what I get in the text file;
440949006E0076006F00690063
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for all the comments.
I'm trying the VB script first. I like the oSQL solution too.
I can't believe it is such a pain though.
The sp_makewebtask worked provided the script was run on the same server as the database and the output saved to that server, if the query is distributed or I save to a UNC path if get 'Remote access not allowed for Windows NT user activated by SETUSER.' errors even though.
I'm trying the VB script first. I like the oSQL solution too.
I can't believe it is such a pain though.
The sp_makewebtask worked provided the script was run on the same server as the database and the output saved to that server, if the query is distributed or I save to a UNC path if get 'Remote access not allowed for Windows NT user activated by SETUSER.' errors even though.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hilaire -- Very intersting.. do you know of a reference that documents this technique?
Jay
Jay
ASKER
Forgot to say thanks to you all.
Hilaire's approach proved to be the most elegant.
Regards
Chris McGuigan
Hilaire's approach proved to be the most elegant.
Regards
Chris McGuigan
@Jay
>>do you know of a reference that documents this technique? <<
No sorry, my references are SQL Server Books OnLine (AKA Transact-SQL help) and EE Knowledge base.
I built this one a while ago for an EE member that needed to get FOR XML output inside a T-SQL variable.
I happen to use sp_OA* methods to send mail whithout bothering with SMTP relay , outlook account, ...
The most tricky thing with the sample code above is to call methods that return objects.
You'll find more exemples on the EE boards.
Regards
Hilaire
>>do you know of a reference that documents this technique? <<
No sorry, my references are SQL Server Books OnLine (AKA Transact-SQL help) and EE Knowledge base.
I built this one a while ago for an EE member that needed to get FOR XML output inside a T-SQL variable.
I happen to use sp_OA* methods to send mail whithout bothering with SMTP relay , outlook account, ...
The most tricky thing with the sample code above is to call methods that return objects.
You'll find more exemples on the EE boards.
Regards
Hilaire
one minor enhancement to hill solution may be to test for file existance
with xp_fileexist stored proc
rather than exec master..xp_cmdshell 'type "c:\temp\test2.xml"'
with xp_fileexist stored proc
rather than exec master..xp_cmdshell 'type "c:\temp\test2.xml"'
dts should work fine
Jay