RE: Output the results of an SP to XML doc
- From: Dave Z. <DaveZ@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Thu, 9 Nov 2006 14:05:01 -0800
I'm not sure how many XML documents your sp is supposed to return, but one
thing I noticed was that you don't increment your recordset index:
while(!rs.eof)In that block you are always concatenating row 0 to row 0 until you get to
{
XMLToReturn = XMLToReturn + rs(0);
rs.movenext
}
the end of the recordset. I don't know if that's the problem, but it's the
only thing that jumped out at me...
--
Dave Zentrich
Data and Security Administrator
Wick Building Systems, Inc.
"JJ" wrote:
Hi,.
I am trying to automate the output of a SQL table to a valid XML document. I
have created a stored procedure that returns valid XML code using FOR XML
EXPLICIT. The next step would be to schedule this uotput into a .xml
document. I have tried to use some javascript, but although the file is
created, it remains empty.
Can anyone help me out here as my VB or .NET skills are not at all
good......
The script looks like this:
//set date
var DateToProcess = GetDate();
//run SP to get XML string
var XML = GetXMLString(DateToProcess);
//write the XML string to file
WriteXMLToFile(XML,DateToProcess);
//exit script reporting success
WScript.Quit(0);
/*
====================================================================
= FUNCTIONS =
====================================================================
*/
function GetDate(){
if (WScript.Arguments.length < 1)
{
//if no date was passed default to yesterday
var dt = new Date();
dt.addDate(-1);
dt.setHours(0,0,0,0);
var DateToReturn = dt.toString();
} else {
//otherwise use the date passed
var DateToReturn = WScript.Arguments(0);
}
WScript.Echo("PROCESSING DATE : " + DateToReturn);
return DateToReturn;
}
function GetXMLString(strDate) {
var XMLToReturn = "";
WScript.Echo("OPENING CONNECTION");
//PROVIDE CONNECTION INFO AND OPEN CONNECTION
var connString = "Provider=SQLOLEDB.1;Persist Security
Info=False;User ID=[username];Password=[password];Initial Catalog=[database
name];Data Source=[sql server]";
var conn = new ActiveXObject("ADODB.Connection");
conn.Open(connString); // open connection
//WScript.Echo(conn);
var rs = new ActiveXObject("ADODB.Recordset");
rs.Open("EXEC sp_export_xml '"+strDate+"'", conn);
while(!rs.eof)
{
XMLToReturn = XMLToReturn + rs(0);
rs.movenext
}
WScript.Echo("FINISHED EXECUTING COMMAND");
//CLOSE THE CONNECTION AND CLEAN UP OBJECTS NOW THAT WE ARE
FINISHED
rs.close();
conn.close();
return XMLToReturn;
}
function WriteXMLToFile(strXML,strDate){
WScript.Echo("WRITING FILE " + "C:\\TEST_"+strDate+".xml");
//CREATE FILE TO WRITE TO
var oFS = new ActiveXObject("Scripting.FileSystemObject");
//file system object
var oFile = oFS.CreateTextFile("c:\\TEST_" + strDate + ".xml",
true); //file to write to, overwites present file
//WRITE XML TO THE FILE
oFile.WriteLine(strXML);
//CLOSE THE FILE AND CLEAN UP OBJECTS NOW THAT WE ARE FINISHED
oFile.close();
oFile = null;
oFS = null;
}
- References:
- Prev by Date: RE: Thoughts on storing address data with XML data type?
- Next by Date: RE: Thoughts on storing address data with XML data type?
- Previous by thread: Output the results of an SP to XML doc
- Next by thread: Failed to load Msxml2.dll
- Index(es):
Relevant Pages
|
Loading