Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2015-02-18
6
Medium Priority
?
218 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 59

Assisted Solution

by:Bill Prew
Bill Prew earned 1000 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
 
LVL 1

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 1000 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
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!

 
LVL 1

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
 
LVL 1

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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

Recently I finished a vbscript that I thought I'd share.  It uses a text file with a list of server names to loop through and get various status reports, then writes them all into an Excel file.  Originally it was put together for our Altiris server…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

916 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