Solved

How to properly perform a sql select into a csv file with VBscript

Posted on 2015-02-18
6
209 Views
Last Modified: 2015-02-19
Hello,

I need to perform to query out a csv file through Vbscript however the query select more than 2000 rows and the following code is not able to extract so many rows into a csv.

Could someone provide a Vbscript to make quickly a query out of large amount of rows or optimize the following code and include the headers.

On Error Resume Next

Dim data
Dim recordCount
Dim regEx

Set regEx = new RegExp
regEx.Pattern = "\r|\n|,|"""

Set con = CreateObject("ADODB.Connection")
con.Open "Driver={SQL Server};" & _
         "Server=;" & _
         "UID=;" & _
         "PWD=;" & _
         "Database=;"

wscript.echo "CONNECTION OK"

strQry = "SELECT * FROM Projects"

Set data = con.execute(strQry)

Set filsSysObj = CreateObject("Scripting.FileSystemObject")

If filsSysObj.FileExists("C:\test\CSVFile2.csv") Then filsSysObj.DeleteFile "C:\test\CSVFile2.csv"

Set csvFile = filsSysObj.OpenTextFile("C:\test\CSVFile2.csv", 8, True)

recordCount = data.Fields.Count

Do until data.EOF
    Separator = ""

    For i = 0 To data.Fields.Count - 1
        Column = data.Fields( i ).Value & ""

        If regEx.Test( Column ) Then
            Column = """" & Replace( Column, """", """""" ) & """"
        End If

        csvFile.Write Separator & Column
        Separator = ";"
    Next

    csvFile.Write vbNewLine
    data.MoveNext
Loop

wscript.echo "Done"

Open in new window

0
Comment
Question by:LD16
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 56

Assisted Solution

by:Bill Prew
Bill Prew earned 250 total points
ID: 40616393
Can't test this easily here, but here are some thoughts.  Try using an actual recordset object for the results, that may help.

Also, I tried to add the logic to write out the comumn names as a header line in the CSV, may need a little tweeking.

On Error Resume Next

Dim data
Dim recordCount
Dim regEx

Set regEx = new RegExp
regEx.Pattern = "\r|\n|,|"""

Set con = CreateObject("ADODB.Connection")
con.Open "Driver={SQL Server};" & _
         "Server=;" & _
         "UID=;" & _
         "PWD=;" & _
         "Database=;"

wscript.echo "CONNECTION OK"

strQry = "SELECT * FROM Projects"

Set data = CreateObject("ADODB.Recordset")
Set data.open strQry, con

Set filsSysObj = CreateObject("Scripting.FileSystemObject")
If filsSysObj.FileExists("C:\test\CSVFile2.csv") Then filsSysObj.DeleteFile "C:\test\CSVFile2.csv"
Set csvFile = filsSysObj.OpenTextFile("C:\test\CSVFile2.csv", 8, True)

recordCount = data.Fields.Count

' Write column headers
hdr = ""
Separator = ""
for i = 0 To data.Fields.Count - 1
    hdr = Separator & """" & data.Fields( i ).Name & """"
    Separator = ";"
Next
csvFile.WriteLine hdr

Do until data.EOF
    Separator = ""

    For i = 0 To data.Fields.Count - 1
        Column = data.Fields( i ).Value & ""

        If regEx.Test( Column ) Then
            Column = """" & Replace( Column, """", """""" ) & """"
        End If

        csvFile.Write Separator & Column
        Separator = ";"
    Next

    csvFile.Write vbNewLine
    data.MoveNext
Loop

csvFile.Close
data.Close
con.Close

wscript.echo "Done"

Open in new window

~bp
0
 

Author Comment

by:LD16
ID: 40616981
Hello Bill,

I have the same problem, I think this code suit just for filtered sql request.
Can we contemplate the option of powershell or bat?

Regards,
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 250 total points
ID: 40618233
Hi, I haven't tested this at all, but can you see what it does for you?

Regards,

Rob.

Dim data
Dim recordCount
Dim con
Dim strQuery
Dim filsSysObj
Dim csvFile
Dim strHeader
Dim Separator
Dim i
Dim strLine
Dim Column

Set con = CreateObject("ADODB.Connection")
con.Open "Driver={SQL Server};" & _
         "Server=;" & _
         "UID=;" & _
         "PWD=;" & _
         "Database=;"

wscript.echo "CONNECTION OK"

strQry = "SELECT * FROM Projects"
Set data = con.execute(strQry)

Set filsSysObj = CreateObject("Scripting.FileSystemObject")
Set csvFile = filsSysObj.CreateTextFile("C:\test\CSVFile2.csv", True)

recordCount = data.Fields.Count

strHeader = ""
Separator = ";"
For Each objField In data.Fields
	If strHeader = "" Then
		strHeader = objField.Name
	Else
		strHeader = strHeader & Separator & Replace(Replace(Replace(Replace(Replace(objField.Name, vbCrLf, ""), vbCr, ""), vbLf, ""), ",", ""), """", "")
	End If
Next
csvFile.WriteLine strHeader

Do until data.EOF
	strLine = ""
    For i = 0 To data.Fields.Count - 1
        Column = Replace(Replace(Replace(Replace(Replace(data.Fields( i ).Value, vbCrLf, ""), vbCr, ""), vbLf, ""), ",", ""), """", "")
        If strLine = "" Then
        	strLine = Column
        Else
        	strLine = strLine & Separator & Column
        End If
    Next

    csvFile.WriteLine strLine
    data.MoveNext
Loop

wscript.echo "Done"

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:LD16
ID: 40618427
Hello Rob,

I got an error message on line 22 char 15.

Thank you in advance for your help.
0
 

Author Comment

by:LD16
ID: 40618434
My mistake, the last code works perfectly.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 40618442
Great. Thanks for the grade. I'm not entirely sure of the difference but I think your Write calls must have been confusing the script, or using too many resources. I prefer to get data as a full line before writing it.

Regards,

Rob.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

623 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