Solved

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

Posted on 2015-02-18
6
189 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
  • 3
  • 2
6 Comments
 
LVL 52

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Unlike scripting languages such as C# where a semi-colon is used to indicate the end of a command, Microsoft's VBScript language relies on line breaks to determine when a command begins and ends. As you can imagine, this quickly results in messy cod…
This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

929 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

14 Experts available now in Live!

Get 1:1 Help Now