• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 254
  • Last Modified:

csv file to sql server

I want to migrate all the data from a CSV file to a new table in SQL.  However, I want to migrate only the records that are new.  There is an ID column that is unique to each record in my CSV file.  How do I import my data from CSV to SQL each time?
0
al4629740
Asked:
al4629740
2 Solutions
 
liranp1Commented:
hi

i don't know much about DB's but from what i know you need to transfer the CSV to XML and then import the XML to SQL DB.
0
 
al4629740Author Commented:
how do I transfer it to xml?
0
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
JHolycloudCommented:
Here is the simplest idea :
  insert all your data from CSV file into temp table, from that temp table you could select the data which match your terms than insert them into your real table.
0
 
mimran18Commented:
Here is the sample
csvtest.txt
Drop TABLE CSVTest
Go
CREATE TABLE CSVTest
(ID INT,
FirstName VARCHAR(40),
LastName VARCHAR(40),
BirthDate SMALLDATETIME)
GO
Drop TABLE CSVtemp
Go
CREATE TABLE CSVtemp
(ID INT,
FirstName VARCHAR(40),
LastName VARCHAR(40),
BirthDate SMALLDATETIME)
GO
BULK
INSERT CSVtemp
FROM 'c:\csvtest.txt'
WITH
(
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)
GO
--Check the existing data of the table and insert new records.
Insert into CSVTest SELECT *
FROM CSVtemp Where [ID] Not In (Select [ID] from CSVTest)
GO
--Select the main table.
SELECT *
FROM CSVTest
GO

Open in new window

0
 
Alpesh PatelAssistant ConsultantCommented:
OK Create SSIS Package and use the Look for Check ID is available in Destination if not then insert else do nothing.

0

Featured Post

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Tackle projects and never again get stuck behind a technical roadblock.
Join Now