Solved

vbscript sql query write to file

Posted on 2010-11-22
13
826 Views
Last Modified: 2012-05-10
Hi,

I have a vbscript used to run a sql query...
In the past I have the whole results  placed in memory  (variable) and then write it to a file in the end.
I need to change this to write to the file as it reads it (writeline...comma separated)

The sql query returns 10 columns and hundreds of rows

anyone have any idea
(the old  piece of code for the sql query section is below)
Set objConn = CreateObject("ADODB.Connection")

objConn.ConnectionTimeout = 0              

objConn.CommandTimeout = 0

Set objRS = CreateObject("ADODB.Recordset")

objConn.Open StrConnectStringset objRS = objConn.Execute (arrlist)

objRS.MoveFirst



intCount = 0

Do While Not objRS.EOF

nRow = nRow + 1

For i = 0 to objRS.Fields.Count - 1 

stat1 = stat1 & objRS.Fields(i) & "," 

f.WriteLine stat1

Next

Open in new window

0
Comment
Question by:neoptoent
  • 6
  • 5
  • 2
13 Comments
 
LVL 16

Expert Comment

by:EvilPostIt
Comment Utility
You could always just directly export it using bcp out.

http://msdn.microsoft.com/en-us/library/ms162802.aspx
0
 

Author Comment

by:neoptoent
Comment Utility
Nah,

 I need to use a specific query that changes earlier in the script..
0
 
LVL 16

Expert Comment

by:EvilPostIt
Comment Utility
You can do that too. Let me know if you change your mind.
0
 
LVL 7

Expert Comment

by:mmr159
Comment Utility
So you have it already working (building the entire result set, then writing the file)??

All you have to do now is to write to the file after each row.  I'm not sure what else you need.


http://blogs.msdn.com/b/webdav_101/archive/2008/03/12/howto-write-a-file-using-vbscript.aspx
0
 

Author Comment

by:neoptoent
Comment Utility
I cant firgure out where to do the writeline and then have it go to the next row
0
 
LVL 7

Expert Comment

by:mmr159
Comment Utility
From the bit of code you posted, it appears like you're close.  Please post all of the code... at least the part that includes all of the FSO stuff.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:neoptoent
Comment Utility
I have the fso part
Set f = fso.OpenTextFile(SMPath & "\Provider\DB\AllSecurityEventsalert.log", 2 , True)

The issue I am having is getting the writeline to write line by line.... as opposed to populatin the stat1 variable with all the results of the squery then writing it once to the file

0
 
LVL 7

Expert Comment

by:mmr159
Comment Utility
You have two loops, one for going over the number of records, another inside that loop to go over the number of columns.

1) You should consider eliminating the second loop.  You should instead concatenate the desired fields in one line.  Like

for each record
    record = rs.fields("column1") & "," & rs.fields("column2") & "," & rs.fields("columnX")
next

Doing this will allow your code to survive certain types of schema changes, and is simply the better practice.  You really should never reference fields by ordinal position.

2) It looks like the code does write to file as the recordset is read.  But looking at the code, I would imagine the output looks something like this:

colA, -- record 1
colA,colB, -- record 1
colA,colB,colC, -- record 1
colA,colB,colC,etc... -- record 1
colA, -- record 2
colA,colB, -- record 2
colA,colB,colC, -- record 2
colA,colB,colC,etc... -- record 2

To only write like per record, move the WriteLine under the Next.  You will also have to clear the stat1 variable after that record has been written.

0
 

Author Comment

by:neoptoent
Comment Utility
Currently what happens is the query is executed (I have 13 columns in the select) it put them line after line into stat1 and then writes stat1 to a text file...

I am not sure I understand where in the code to force it to write line after line into the file
0
 
LVL 7

Accepted Solution

by:
mmr159 earned 500 total points
Comment Utility
OK, I think we still have a misunderstanding.

Are you asking to have the code physically write each line to a file *during* execution, so that you might see the code performing while it is running?

OR

Is there simply something wrong with the output, like the records or columns are not properly written by line?

If your issue is the first one, then I don't particularly see the problem.  As far as I know, WriteLine will write in real time.  If it is not, the only thing I can suggest you try is to open, WriteLine, then close the file object every record in the loop.

If your issue is the second one, let me know, and see some more of the code.  To me, you would want something more like the following:


Do While Not objRS.EOF
   variable = objRS.Fields("first_column") & "," & objRS.Fields("second_column") & etc...
   f.WriteLine variable
Next

Open in new window

0
 

Author Comment

by:neoptoent
Comment Utility
The sql query streams into a variable called stat1 right now and then write to a file at the end.
I want the query to writeline to the event file instead of streaming into a variable
0
 
LVL 7

Expert Comment

by:mmr159
Comment Utility
It *should not* be writing the file at the "end".

f is your file object, right?  When you call WriteLine, it should write what you ask it to right then and there.  This is why I don't understand why you're having a problem.

Without the entire code, or preferably a small but complete example that demonstrates your problem, I can't help.
0
 

Author Closing Comment

by:neoptoent
Comment Utility
Some help
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now