Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
CSV to SQL server - best way?
Message
From
16/03/2004 09:12:51
Cetin Basoz
Engineerica Inc.
Izmir, Turkey
 
General information
Forum:
Visual FoxPro
Category:
Databases,Tables, Views, Indexing and SQL syntax
Miscellaneous
Thread ID:
00886406
Message ID:
00886662
Views:
41
>Hi;
>
>We have a fair amount of records to import to SQL from CSV. Should I be importing to a cursor and then use a SCAN loop to T-SQL them into the table, or is there a more direct way to import the whole thing in my VFP client app?
>
>TIA

Michael,
This T-SQL sample might help (but check in help for rights you need - here I used TAB delimited file which is default for bulk insert, see commented section for comma delimited ) :
use employee
COPY fields emp_id, first_name, last_name to SQLBULK.txt TYPE DELIMITED WITH "" WITH TAB

Local lnHandle, lcDatabaseName
lcDatabaseName = "myTestData"
lnHandle=SQLStringConnect('DRIVER=SQL Server;SERVER=servername;Trusted_connection=Yes')
If SQLExec(lnHandle, "create database "+lcDatabaseName) < 0
  Do errHand
  Return
Endif
SQLExec(lnHandle, "use "+lcDatabaseName)
SQLExec(lnHandle, ;
  "create table myTable"+;
  " (empid varchar(6), first varchar(20), last varchar(20))")

* Comma delimited
*!*	cSQL = [BULK INSERT myTestData.dbo.myTable ] + ;
*!*	[FROM ']+sys(5)+curdir()+'SQLBULK.txt'+[' ] + ;
*!*	[WITH (DATAFILETYPE = 'char', ]+;
*!*	[FIELDTERMINATOR = ',', ]+;
*!*	[ROWTERMINATOR = ']+chr(13)+chr(10)+[')]

cSQL = [BULK INSERT ]+lcDataBaseName+[..myTable ] + ;
[FROM ']+sys(5)+curdir()+'SQLBULK.txt'+[' ] + ;
[WITH (DATAFILETYPE = 'char')]

SQLExec(lnHandle, cSQL)
SQLDisconnect(lnHandle)
Also check OpenRowSet and OpenDataSource.
Cetin
Çetin Basöz

The way to Go
Flutter - For mobile, web and desktop.
World's most advanced open source relational database.
.Net for foxheads - Blog (main)
FoxSharp - Blog (mirror)
Welcome to FoxyClasses

LinqPad - C#,VB,F#,SQL,eSQL ... scratchpad
Previous
Reply
Map
View

Click here to load this message in the networking platform