Importing and Exporting CSV Files From
Importing and Exporting CSV Files From
Page 1 of 4
Importing and Exporting CSV Files From SQLServer Using ADODB and
Nerds-Central
VBScript
• The Tsunami Revolution
Podcast - Part Two
• Voice Filtering With Here is a simple pair of scripts to export an SQL Server 2005 table to a csv file and to
Audacity import such a csv file back into SQL Server.
• The Tsunami Revolution
Podcast - End Of The
Personal Computer Today I was faced with a challenge where one of my team members accidentally dropped a table. Restoring the
• MD 686, Retro table structure was no problem because we keep structure scripts, but the data was an issue. We did not want to
Microphone Review - I restore the whole db because it was a development db and was in the middle of being developed upon.
LOVE IT!
• Tips Learnt For However, the table in question was also on another team member's development db, so here are the scripts I
Recording Podcasts came up with the fix the problem. They export the table to a csv file on the Desktop and import the CSV file back
into the repaired database.
The CSV file creation method is very simple. Actually, I think it is too simple because it really does nothing to
take account of non text characters etc. For my table, that was OK; for general use I think some work needs to
Data Export & be done there.
Import Tool
Easy and Fastest Please note that in the reload script I faced two challenges. The first was that the table had an identity column.
way to load data Normally, one cannot set the value of an identity field manually. However, to restore the table values correctly, I
to/from CSV files &
Databases
needed to. The solution was the IDENTITY_INSERT property set (see the code). The second challenge is a bug
www.dbload.com in the ADODB implementation. One of the columns was SQL Variant. Now, "" in the CSV file can be created by
an empty String but is interpreted as NULL. This can be fixed by checking the field value coming from the CSV
derived record set and changing NULL to "". But, due to the bug, "" will not load into the field - arrrrg! The solution
was to substitute a known nonsense String for NULL and then change all instances of that String to '' using an
Compare data of
SQLServer UPDATE SQL statement.
Compare and
Synchronize data Export Script
of SQL Server
quickly
www.devart.com Option Explicit
Dim rs,fieldVals,dbConnIn
Dim connectString,shell,tmp,fso,ts,line
Convert XML to
Excel(XLS) Const adOpenDynamic=2
Shred XML to
Tables. Export to Const adLockPessimistic=2
XLS MDB, CSV. Const adCmdTable=2
Free Trial, US$105 Const adOpenForwardOnly=0
to buy Const adOpenStatic=3
www.novixys.com/Exult
Const adLockReadOnly=1
SQL Scripter ' The connect string should be changed to fit the db in use
Generate T-SQL ' here I am using TCP tp connect to SQL Server 2005
data scripts, connectString="Provider=SQLNCLI;Server=127.0.0.1;Database=dv;Uid=*****;Pwd=*****;"
Export to Set dBConnIn = CreateObject("ADODB.Connection")
CSV/Text, PDF,
Excel, CSV to SQL dBConnIn.Open connectString
www.sqlscripter.com ' This is just a simple way of getting a record set from and SQL Query
Set rs=CreateObject("ADODB.RecordSet")
rs.Open _
Followers
"T_MchnPrps", _
dbConnIn, _
Follow adOpenStatic, _
with Google Friend Connect
adLockReadOnly, _
Followers (5) adCmdTable
Set shell=CreateObject("WScript.Shell")
Set fso=CreateObject("Scripting.FileSystemObject")
Set ts=fso.OpenTextFile(shell.SpecialFolders("Desktop") & "\" & "T_MchnPrps.csv",2,T
line=""
Already a member?Sign in For Each tmp In rs.Fields
line=line & tmp.Name & ","
Dr Alexander J Turner Next
ts.WriteLine Left(line,Len(line)-1)
While Not rs.EOF
line=""
For Each tmp In rs.Fields
line=line & """" & Replace(tmp.Value,"""","""""") & ""","
Next
ts.WriteLine Left(line,Len(line)-1)
rs.MoveNext
Wend
About Me rs.Close
ts.close
http://nerds-central.blogspot.com/2008/01/importing-and-exporting-csv-files-from.html 25-10-2010
Nerds Central: Importing and Exporting CSV Files From SQLServer Using ADODB ... Page 2 of 4
MchnId,Sequ,Type,Data,Prnt
"1","1","1","RBSManuf0","1"
"1","2","2","IBM T42","1"
"1","3","3","RBSSerl0","1"
"2","4","1","RBSManuf1","1"
"2","5","2","IBM T42","1"
"2","6","3","RBSSerl1","1"
"3","7","1","RBSManuf2","1"
"3","8","2","Dell PP04S ","1"
"3","9","3","RBSSerl2","1"
"4","10","1","RBSManuf3","1"
"4","11","2","","1"
"4","12","3","RBSSerl3","1"
"5","13","1","RBSManuf4","1"
Import Script
Option Explicit
Dim rsOut,rsIn,dbConnOut,fieldVals,dbConnIn,i
Dim connectString,shell,tmp,fso,ts,line,fieldPost,fieldValues
Const adOpenDynamic=2
Const adLockPessimistic=2
Const adCmdTable=2
Const adOpenForwardOnly=0
Const adOpenStatic=3
Const adLockReadOnly=1
Set rsOut=CreateObject("ADODB.RecordSet")
rsOut.Open _
"T_MchnPrps", _
dbConnOut, _
adOpenDynamic, _
adLockPessimistic, _
adCmdTable
Set shell=CreateObject("WScript.Shell")
Set rsIn=CreateObject("ADODB.RecordSet")
rsIn.Open _
"T_MchnPrps.csv", _
dbConnIn, _
adOpenStatic, _
adLockReadOnly, _
adCmdTable
Redim fieldPos(rsIn.Fields.Count-1)
Redim fieldValues(rsIn.Fields.Count-1)
i=0
For i=0 To rsIn.Fields.Count-1
fieldPos(i)=i
Next
rsIn.Close
rsOut.Close
dbConnOut.Execute "UPDATE T_MchnPrps SET Data='' WHERE Data='-#%REMOVE%#-'"
dbConnOut.Execute "SET IDENTITY_INSERT T_MchnPrps OFF"
WScript.Echo "Import Complete"
http://nerds-central.blogspot.com/2008/01/importing-and-exporting-csv-files-from.html 25-10-2010
Nerds Central: Importing and Exporting CSV Files From SQLServer Using ADODB ... Page 3 of 4
3 comments:
Anonymous said...
12:26 PM
12:29 PM
Anonymous said...
Although if you use a view, views don't sort, even though they look like they should, so you have to sort the view
once you have the view results back.
'CONNECT TO DATABASE
set oConn = CreateObject("ADODB.Connection")
oConn.Open "your connection goes here"
set rs = CreateObject("ADODB.Recordset")
rs.ActiveConnection = oConn
dim strsql
strsql = "select * from vwMyView order ID ASC"
rs.Source = strsql
rs.open()
Set shell=CreateObject("WScript.Shell")
etc. as before
12:40 PM
Post a Comment
http://nerds-central.blogspot.com/2008/01/importing-and-exporting-csv-files-from.html 25-10-2010
Nerds Central: Importing and Exporting CSV Files From SQLServer Using ADODB ... Page 4 of 4
http://nerds-central.blogspot.com/2008/01/importing-and-exporting-csv-files-from.html 25-10-2010