Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
steelkilt
Constraint Violating Yak Guru
255 Posts |
Posted - 2003-01-13 : 09:37:05
|
| Hi. I'm using a nice script from learnasp.com to send the results of an SPROC call to a CSV file. I have made minor modifications to the script to handle SPROC parameters and to redirect users to the newly-created CSV. My problem is that the CSV output lists only the records and not the Field titles. Can anyone see what's missing in the code below? thx.==============================================================<%@ LANGUAGE="VBSCRIPT" %><!-- #include FILE="adovbs.inc" --><html><head><TITLE>MLtest to CSV</TITLE></head><body bgcolor="#FFFFFF"><%'Collect the needed form fields strTableChoice = cStr(Request.Form("TableChoice")) strBeginRange = cStr(Request.Form("BeginRange")) strEndRange = cStr(Request.Form("EndRange"))%><%whichname="/temp/mltest.csv"myDSN="stats"mySQL="sp_mltest_results_range '" & strTableChoice &"','" & strBeginRange &"', '" & strEndRange &"'"showblank=""shownull="<null>"linestart=chr(34)lineend=chr(34)delimiter=chr(34) & "," & chr(34)delimitersub=""whichFN=server.mappath(whichname)Set fstemp = server.CreateObject("Scripting.FileSystemObject")Set filetemp = fstemp.CreateTextFile(whichFN, true)' true = file can be over-written if it exists' false = file CANNOT be over-written if it existsset conntemp=server.createobject("adodb.connection")conntemp.open myDSNset rstemp=conntemp.execute(mySQL)' this code detects if data is emptyIf rstemp.eof then response.write "No data to convert for SQL statement<br>" response.write mySQL & "<br>" connection.close set connection=nothing response.endend ifDO UNTIL rstemp.eof thisline=linestart for each whatever in rstemp.fields thisfield=whatever.value if isnull(thisfield) then thisfield=shownull end if if trim(thisfield)="" then thisfield=showblank end if thisfield=replace(thisfield,delimiter,delimitersub) thisline=thisline & thisfield & delimiter next tempLen=len(thisline) tempLenDelim=len(delimiter) thisline=mid(thisline,1,tempLEN-tempLenDelim) & lineend filetemp.WriteLine(thisline) ' response.write thisline & "<br>" rstemp.movenextLOOPfiletemp.Closeset filetemp=nothingset fstemp=nothingrstemp.closeset rstemp=nothingconntemp.closeset conntemp=nothingIf err.number=0 then response.write "File was converted sucessfully!<br>" response.write "Converted file is at <a href='" response.write whichname & "'>" & whichname & "</a>"else response.write "VBScript Errors Occured!<br>" response.write "Error Number=#<b>" & err.number & "</b><br>" response.write "Error Desc. =<b>" & err.description & "</b><br>" response.write "Help Path =<b>" & err.helppath & "</b><br>" response.write "Native Error=<b>" & err.nativeerror & "</b><br>" response.write "Error Source =<b>" & err.source & "</b><br>" response.write "SQL State=#<b>" & err.sqlstate & "</b><br>"end ifResponse.redirect ("http://dbserver/temp/mltest.csv")%></body></html> |
|
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2003-01-13 : 10:07:30
|
| You got this script from http://www.learnasp.com/ and it DOESN'T use GetString or GetRows? Charles must've gotten hit on the head with a blunt object and lost his memory.This will not only write out the column names, but will be MUCH faster than what he had earlier:<%@ LANGUAGE="VBSCRIPT" %> <!-- #include FILE="adovbs.inc" --> <html><head> <TITLE>MLtest to CSV</TITLE> </head><body bgcolor="#FFFFFF"> <% 'Collect the needed form fields strTableChoice = cStr(Request.Form("TableChoice")) strBeginRange = cStr(Request.Form("BeginRange")) strEndRange = cStr(Request.Form("EndRange")) whichname="/temp/mltest.csv" myDSN="stats" mySQL="sp_mltest_results_range '" & strTableChoice &"','" & strBeginRange &"', '" & strEndRange &"'" showblank="" shownull="<null>" linestart=chr(34) lineend=chr(34) & vbCRLF 'This is changed from the originaldelimiter=chr(34) & "," & chr(34) delimitersub="" whichFN=server.mappath(whichname) set conntemp=server.createobject("adodb.connection") conntemp.open myDSN set rstemp=conntemp.execute(mySQL) ' this code detects if data is empty If rstemp.eof then response.write "No data to convert for SQL statement<br>" response.write mySQL & "<br>" connection.close set connection=nothing response.end end if data=linestart 'starts the data value with a delimiterfor each whatever in rstemp.fields 'for each field in the recordsetdata=data & whatever.name & delimiter 'add the field name and delimit itnext'trim the last delimiter and add the lineenddata=Left(data, Len(data)-Len(delimiter)) & lineend 'convert the recordset to a string using the delimiter, lineend, and shownull replacementsdata=data & rstemp.GetString(,, delimiter, lineend & linestart, shownull) rstemp.close set rstemp=nothing conntemp.close set conntemp=nothing Set fstemp = server.CreateObject("Scripting.FileSystemObject") Set filetemp = fstemp.CreateTextFile(whichFN, true) ' true = file can be over-written if it exists ' false = file CANNOT be over-written if it exists filetemp.Write(Left(data, Len(data)-Len(linestart)) 'trims the last linestart value and writes the file' response.write thisline & "<br>" filetemp.Close set filetemp=nothing set fstemp=nothing If err.number=0 then response.write "File was converted sucessfully!<br>" response.write "Converted file is at <a href='" response.write whichname & "'>" & whichname & "</a>" else response.write "VBScript Errors Occured!<br>" response.write "Error Number=#<b>" & err.number & "</b><br>" response.write "Error Desc. =<b>" & err.description & "</b><br>" response.write "Help Path =<b>" & err.helppath & "</b><br>" response.write "Native Error=<b>" & err.nativeerror & "</b><br>" response.write "Error Source =<b>" & err.source & "</b><br>" response.write "SQL State=#<b>" & err.sqlstate & "</b><br>" end if Response.redirect ("http://dbserver/temp/mltest.csv") %> </body></html> |
 |
|
|
steelkilt
Constraint Violating Yak Guru
255 Posts |
Posted - 2003-01-13 : 10:28:25
|
| Thanks, Rob. Works nicely. Quick ones:1. The very first entry in the CSV lists the field value followed by " How can I get rid of that?2. Your original code was missing a bracket at the end of filetemp.write. I added as below:filetemp.Write(Left(data, Len(data)-Len(linestart)))3. If I want to have a unique filename created each time the user runs this script, would I use VBS Response object GetTempName?I should have mentioned that this example came from the Classic ASP section of archived materials, so this may explain Charles' use of the classic coding technique.Thanks again for the help. |
 |
|
|
steelkilt
Constraint Violating Yak Guru
255 Posts |
Posted - 2003-01-13 : 11:31:27
|
| Rob,I figured it out. Needed another linestart before GetString, as below. Thanks again.=========================================================='convert the recordset to a string using the delimiter, lineend, and shownull replacementsdata=linestart 'starts the data value with a delimiterdata=data & rstemp.GetString(,, delimiter, lineend & linestart, shownull) |
 |
|
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2003-01-13 : 11:40:41
|
quote: 1. The very first entry in the CSV lists the field value followed by " How can I get rid of that?
Can you show me an example of the output? Just a couple of lines. As usual I can't quite see the problem in the code itself (it's there, it's just hiding)  quote: 2. Your original code was missing a bracket at the end of filetemp.write. I added as below:filetemp.Write(Left(data, Len(data)-Len(linestart)))
    Must. Have. Coffee. Before. Typing. Code.quote: 3. If I want to have a unique filename created each time the user runs this script, would I use VBS Response object GetTempName?
Actually it's a method of the FileSystemObject, not the Reponse object, but yeah you could definitely use that.quote: I should have mentioned that this example came from the Classic ASP section of archived materials, so this may explain Charles' use of the classic coding technique.
You must've found the one article he didn't go back and patch, he was pretty good at linking his GetRows stuff to everything else that had a recordset loop in it. This is the equivalent of an article written by NR on the joys of cursor usage! Must. Preview. Thread. Before. Posting.Edited by - robvolk on 01/13/2003 11:42:17 |
 |
|
|
steelkilt
Constraint Violating Yak Guru
255 Posts |
Posted - 2003-01-13 : 16:13:02
|
| But wait! I spoke too soon. Adding the second linestart just removed my first row of fields altogether in the CSV output! I can't figure out where the bug is, but I can see that the very first record displays with a " to the right of the value in the CSV output, like this:101"so it seems that the trimming of linestart may be the culprit, but, again, I can't see where to modify the code.thx |
 |
|
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2003-01-13 : 16:23:43
|
| Try this:data=data & linestart & rstemp.GetString(,, delimiter, lineend & linestart, shownull) |
 |
|
|
steelkilt
Constraint Violating Yak Guru
255 Posts |
Posted - 2003-01-13 : 16:30:44
|
| Thx. It works. So, before you GetString, you drop in another linestart, thus providing the missing " for the first cell in the first row after fields are collected. Got it. |
 |
|
|
|
|
|
|
|