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
 General SQL Server Forums
 New to SQL Server Programming
 Help, bulk import data from .csv to sql

Author  Topic 

JaseDytec
Starting Member

14 Posts

Posted - 2008-08-04 : 06:32:12
Hi ppl.

I'm new to programming, sql server and having a few problems with creating a query to bulk import data from a csv file to sql database. How would i modify the following query so that SQL Express can automatically insert an ID to the data. There are topics on this on the msdn but they are to hard for a novice to follow.


USE [HumsDataCentre]

GO

--Add data
BULK
INSERT ETI
FROM '(file path)'
WITH
(
FIRSTROW = 2,
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)

GO

SELECT *
FROM ETI
GO

The CSV file looks like this

Device,Duration,Units
A, 5, hours

My sql table has the following fields

Device ID, Device, Duration, Units


Please help, thanks Jason.


matty
Posting Yak Master

161 Posts

Posted - 2008-08-04 : 06:41:05
Set Device ID as an identity column
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-08-04 : 13:19:17
Use
[Device ID] int IDENTITY(1,1)
in table definition
Go to Top of Page

JaseDytec
Starting Member

14 Posts

Posted - 2008-08-11 : 08:37:10
Sorry i wasn't too clear, i need to tell the bulk insert to copy the data into the last three columns, Device, Duration, Units so that SQL can auto insert an ID.
Go to Top of Page

JaseDytec
Starting Member

14 Posts

Posted - 2008-08-11 : 08:39:31
I think i need to use a format file, but i dont know how to?
Go to Top of Page
   

- Advertisement -