Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


Save result sets to a text file

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,60454
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Save result sets to a text file
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 Save result sets to a text file - 5/20/2008 1:54:45 AM   
  acidedge2004

 

Posts: 5
Score: 0
Joined: 5/20/2008
Status: offline
I am trying to write a script that will execute SQL against each database in the system, then outputting the result sets to a text file. Here is what I have so far:

Code: Dim objFile
Dim conn, rs1, rs2
Dim DBName, textfile, dbid

Set conn = CreateObject("ADODB.Connection")
conn.Open "Driver={SQL Native Client};Server=" & WScript.Arguments(0) & ";Database=EMISSYSTEM;Trusted_Connection=yes;"

Set rs1 = conn.Execute("SET NOCOUNT ON SET ANSI_NULLS OFF select DBNAME from MainDB..SITE_DETAILS WHERE DBNAME <> NULL")


if rs1.EOF then
WScript.Echo "No practices found"
WScript.Quit 0
End If


set objFile= CreateObject ("Scripting.FileSystemObject")
Set textfile = objFile.CreateTextFile("Results.txt")

Do Until rs1.EOF

DBName = rs1.Fields.Item("DBName").value
WScript.Echo DBName

set rs2 = conn.Execute("Select * from"&DBName&"..SITE_DETAILS")

If rs2.EOF then WScript.Echo "No results"

Do Until rs2.EOF
textfile.WriteLine(rs2)
textfile.WriteLine("")
loop

rs1.MoveNext
loop


set rs1 = Nothing
set rs2 = Nothing

textfile.Close
conn.close
set conn = Nothing

The result set rs1 is basically a list of all the databases I want to run the SQL against. So rs2 should basically run "Select * from Database1..SITE_DETAILS" then loop through again with Database2, Database3 etc. With each iteration I then want the results of rs2 to be printed to the textfile. I get 3 error messages with this script.

1. "A loop without a do statement" and it points to the loop for the Do Until rs1.EOF. When I comment out the second Do loop the error message goes away an the script runs if the inside loop is just a print statement.
2. That I cannot write rs2 to the text file as it is an object.
3. "DBName..SITE_DETAILS" is displayed as it should be in the error message, so shows "Database1..SITE_DETAILS" but then says the object is incorrect. But it is definitely correct.

I am assuming that the way I have gone about this is correct except for something relatively easy to fix, so if someone can point to the problem and direct me to a solution I would be extremely grateful!
 
 
Post #: 1
 
 RE: Save result sets to a text file - 5/20/2008 1:41:25 PM  1 votes
  dm_4ever


Posts: 2665
Score: 46
Joined: 6/29/2006
From: Orange County, California
Status: offline
Completely untested....


      

_____________________________

dm_4ever

My philosophy: K.I.S.S - Keep It Simple Stupid
Read Me: http://www.visualbasicscript.com/m_24727/tm.htm
Frequently Asked Stuff: http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to acidedge2004)
 
 
Post #: 2
 
 RE: Save result sets to a text file - 5/20/2008 7:50:54 PM   
  acidedge2004

 

Posts: 5
Score: 0
Joined: 5/20/2008
Status: offline
It works man, thanks! It was driving me nuts. I am new to VB but a you can see I am writing some stuff that can get messy. Cheers!!!

(in reply to dm_4ever)
 
 
Post #: 3
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Save result sets to a text file Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts