Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Import CSV file to SQL Server Database

Author  Topic 

blinton25
Yak Posting Veteran

90 Posts

Posted - 2004-06-24 : 12:58:56
Hello,

How are you today?

I have an Access application which uses linked tables on SQL Server 2K. I have the task of importing data from a .csv file to a database table.
I currently accomplish this using the following:


Public Function AppendFile2()
Dim FileString As String
Dim Fpath, Fname As String
Dim tbl As TableDef
Dim tdfLinked As TableDef
Dim fnstart As Integer

Dim dbs As Database

Set dbs = CurrentDb()

FileString = FileOpen()

If FileString = "" Then
Exit Function
End If

fnstart = InStr(ReverseString(FileString), "\")

Fpath = Left$(FileString, Len(FileString) - fnstart)
Fname = Right$(FileString, fnstart - 1)

On Error Resume Next
dbs.TableDefs.Delete "Input Table Temp"
On Error GoTo 0

Set tdfLinked = dbs.CreateTableDef("Input Table Temp")

tdfLinked.Connect = "Text;DATABASE=" & Fpath
tdfLinked.SourceTableName = Fname
dbs.TableDefs.Append tdfLinked

On Error GoTo 0

DoCmd.RunMacro "Append Input Table Macro1"
Exit Function


Once the file is imported I then append the contents to an existing table:


INSERT INTO [Input table] ( C_NUMBER, F_NUMBER )
SELECT [Input Table Temp].CARD, [Input Table Temp].F_NO
FROM [Input Table Temp];


I am now converting my queries to passthrough ones to take advantage of the power of my database server, but since [Input Table Temp] is a temporary table in my Access database, my append query is not working. Is there another way way to acheive this import?

nr
SQLTeam MVY

12543 Posts

Posted - 2004-06-24 : 13:03:05
Why not just use bulk insert?

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

blinton25
Yak Posting Veteran

90 Posts

Posted - 2004-06-24 : 15:45:31
Hi,

Thanks, that looks like a good suggestion. My .csv file is going to be on a different machine to the database on the server, and the location and filename varies, how do I handle these issues?
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2004-06-24 : 16:05:15
You need to copy the file to the database server or use a UNC path. But you have to keep in mind that the path that you pass BULK INSERT must be from the database server's perspective.

Tara
Go to Top of Page

blinton25
Yak Posting Veteran

90 Posts

Posted - 2004-06-25 : 11:43:29
Hello,

Issue is I can't see my end users copying the file to the Server, ideally I am looking for an approach similar to what is done now, where they browse their hard drive, select the .csv file, and it is imported into the database table.
Go to Top of Page
   

- Advertisement -