Solved

Import CSV file in SQL using Bulk insert

Posted on 2016-11-03
4
38 Views
Last Modified: 2016-11-03
Hello,

I have created the table that match exactly the header of the CSV file but when I  run the bulk insert query it returns ( 0 row(s) afftected)
CREATE TABLE [dbo].[TCountry](
	[CountryCode] [varchar](3) NULL,
	[IsValid] [char](1) NULL,
	[CountryDesignation] [varchar](150) NULL,
	[LCC] [char](1) NULL,
	[RegionCode] [nchar](10) NULL,
	[User] [varchar](50) NULL,
	[UpdateDateTime] [varchar](50) NULL
) ON [PRIMARY]

bulk insert TCountry from 'C:\COUNTRY.csv'
with ( FIELDTERMINATOR =';', FIRSTROW = 2 )

Open in new window

COUNTRY.csv
0
Comment
Question by:arnololo123
  • 2
4 Comments
 
LVL 47

Expert Comment

by:Vitor Montalvão
ID: 41872322
How did you create the CSV file?
It might need a ROWTERMINATOR also.
0
 
LVL 47

Accepted Solution

by:
Vitor Montalvão earned 500 total points
ID: 41872326
Try with the ROWTERMINATOR:
bulk insert TCountry from 'C:\temp\COUNTRY.csv'
with ( FIELDTERMINATOR =';', ROWTERMINATOR='0x0a',FIRSTROW = 2)

Open in new window

0
 
LVL 1

Expert Comment

by:PAGANED
ID: 41872345
Your problem stems from your CSV file
.
Change your CSV file into a TXT file , and it will work fine
Make sure the file at the location "C:\"  is on the SQL Server !
..........
bulk insert TCountry from 'C:\COUNTRY.txt'
with ( FIELDTERMINATOR =';', FIRSTROW = 2 )
...........
CountryCode;IsValid;CountryDesignation;LCC;RegionCode;User;UpdateDateTime
ABW;Y;Aruba;Y;ROW;youbi;2015-08-11T16:04:08
AFG;Y;Afghanistan;Y;ASI;youbi;2015-08-11T16:04:08
AGO;Y;Angola, Republic of;Y;ROW;youbi;2015-08-11T16:04:08
AIA;Y;Anguilla;Y;ROW;youbi;2015-08-11T16:04:08
ALB;Y;Albania, People's Socialist Republic of;Y;EUR;youbi;2015-08-11T16:04:08
AND;Y;Andorra, Principality of;Y;EUR;youbi;2015-08-11T16:04:08
ANT;Y;Netherlands Antilles;Y;ROW;youbi;2015-08-11T16:04:08
ARE;Y;United Arab Emirates;Y;ASI;youbi;2015-08-11T16:04:08
ARG;Y;ARGENTINA;Y;SAM;youbi;2015-08-11T16:04:08
ARM;Y;Armenia;Y;ASI;youbi;2015-08-11T16:04:08
ASM;Y;American Samoa;Y;ROW;youbi;2015-08-11T16:04:08
ATA;Y;Antarctica (the territory South of 60 deg S);Y;ROW;youbi;2015-06-16T15:49:31
ATF;Y;French Southern Territories;Y;ROW;youbi;2015-06-16T15:49:31
ATG;Y;Antigua and Barbuda;Y;ROW;youbi;2015-08-11T16:04:08
AUS;Y;Australia, Commonwealth of;N;ROW;youbi;2015-08-11T16:04:08
.
.
0
 

Author Closing Comment

by:arnololo123
ID: 41872365
great thanks
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question