Created
March 3, 2013 19:57
-
-
Save charlesbedrosian/5077992 to your computer and use it in GitHub Desktop.
vbscript/wsh script to dump all tables in the indicated SQL database to CSV files. Replaces CrLf with \r\n and replaces NULL values with <null> string
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Option Explicit | |
Dim server_name, db_name | |
Dim cs, cn, rsTables, rsData, sql, rsCount | |
Dim line, fso | |
Dim outfile | |
Dim i | |
Dim lineCount, lineTotalCount, baseMessage | |
server_name = "win7b" | |
db_name = "io" | |
cs = "Provider=sqloledb;Data Source=" & server_name & ";Initial Catalog=" & db_name & ";;Integrated Security=SSPI" | |
Set cn = CreateObject("ADODB.Connection") | |
cn.Open cs | |
Set rsTables = CreateObject("ADODB.recordset") | |
sql = "select name as table_name from sys.objects where type = 'U'" | |
rsTables.open sql, cn, 3,3 | |
rsTables.MoveFirst | |
Set fso = CreateObject("Scripting.FileSystemObject") | |
While Not rsTables.EOF | |
set rsCount = cn.Execute("SELECT count(*) FROM " & rsTables("table_name")) | |
lineTotalCount = rsCount(0) | |
rsCount.Close | |
set rsCount = nothing | |
Set outfile = fso.OpenTextFile(rsTables("table_name") & ".csv", 2, true) | |
baseMessage = "Dumping '" & rsTables("table_name") & "'... " | |
ProgressOut baseMessage, 0, lineTotalCount | |
sql = "select * from " & rsTables("table_name") | |
Set rsData = CreateObject("ADODB.Recordset") | |
rsData.open sql, cn, 3, 3 | |
line = "" | |
lineCount = 0 | |
For i = 0 To rsData.fields.count-1 | |
If line <> "" Then | |
line = line & "," | |
End If | |
line = line & rsData.Fields(i).Name | |
Next | |
outfile.WriteLine line | |
line = "" | |
While Not rsData.eof | |
For i = 0 To rsData.fields.count-1 | |
If line <> "" Then | |
line = line & "," | |
End If | |
line = line & """" & PrepareData(rsData(i)) & """" | |
Next | |
rsData.MoveNext | |
On Error Resume Next | |
Err.Clear | |
outfile.WriteLine line | |
If Err.Number <> 0 Then | |
Wscript.Echo"" | |
Wscript.Echo "Error writing line: " & Err.Message | |
Wscript.Echo line | |
Else | |
lineCount = lineCount + 1 | |
ProgressOut baseMessage, lineCount, lineTotalCount | |
End If | |
On Error Goto 0 | |
line = "" | |
Wend | |
outfile.Close | |
Set outfile = Nothing | |
Wscript.Echo "" | |
rsData.Close | |
set rsData = Nothing | |
rsTables.MoveNext | |
Wend | |
rsTables.Close | |
Set rsTables = Nothing | |
cn.Close | |
Set cn = Nothing | |
Sub ProgressOut(ByVal Base, ByVal Current, ByVal Total) | |
WScript.StdOut.Write vbCr & Base & " [" & FormatNumber(Current,0) & "/" & FormatNumber(Total,0) & "]" | |
End Sub | |
Function PrepareData(ByVal s) | |
If (IsNull(s)) Then | |
s = "<null>" | |
Else | |
s = replace(s, """", """""") | |
s = replace(s, "\", "\\") | |
s = replace(s, Chr(13), "\r") | |
s = replace(s, Chr(10), "\n") | |
End If | |
PrepareData = s | |
End Function |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment