Solved

MS SQL Bulk Insert

Posted on 2015-02-01
6
156 Views
Last Modified: 2015-02-02
Hi,
I am getting multiple truncate error messages with this command, the source data on the specific field can normally fit in the destination file. I wonder how MS SQL count the character in csv file.
Bulk Insert dbo.xtemp From 'c:\test\agtest.csv'
     With ( FirstRow =2, FieldTerminator = ',', RowTerminator = '\n' )
In MySQL, I can handle  this with SET command in my load data  infile command.
Is there any equivalent in MS SQL, that can allow to write something like SET field1=left(trim(field1),50)
0
Comment
Question by:Omer-Pitou
  • 3
  • 2
6 Comments
 
LVL 24

Assisted Solution

by:chaau
chaau earned 250 total points
ID: 40583027
I think you need to use the Format File. You can easily create a format file for your table using the bcp utility:
bcp yourDb.dbo.xtemp format nul -f c:\test\xtemp.Fmt -n -T 

Open in new window

After the file is created you can use it with the Bulk Insert command:
BULK INSERT dbo.xtemp From 'c:\test\agtest.csv'
     With ( FirstRow =2, FieldTerminator = ',', RowTerminator = '\n', FORMATFILE = 'c:\test\xtemp.Fmt' ) 

Open in new window

Also, use the DATAFILETYPE parameter to tell the SQL Server what file format you have:
DATAFILETYPE = { 'char' | 'native' | 'widechar' | 'widenative' }

Open in new window

So, if you have a UNICODE file use :
BULK INSERT dbo.xtemp From 'c:\test\agtest.csv'
     With ( FirstRow =2, FieldTerminator = ',', RowTerminator = '\n', FORMATFILE = 'c:\test\xtemp.Fmt',  DATAFILETYPE = 'widechar') 

Open in new window

0
 

Author Comment

by:Omer-Pitou
ID: 40583045
Trying with bcp, I am getting an error message 'could not find stored procedure bcp
0
 
LVL 24

Expert Comment

by:chaau
ID: 40583062
bcp is a Command line utility. You need to run it via cmd.exe.

Anyway, try first with the DATAFILETYPE:
BULK INSERT dbo.xtemp From 'c:\test\agtest.csv'
     With ( FirstRow =2, FieldTerminator = ',', RowTerminator = '\n', DATAFILETYPE = 'widechar') 

Open in new window

0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:Omer-Pitou
ID: 40583069
Giving an error stating data file type was incorrectly specified as widechar
0
 
LVL 45

Accepted Solution

by:
Vitor Montalvão earned 250 total points
ID: 40583498
Is there any equivalent in MS SQL, that can allow to write something like SET field1=left(trim(field1),50)
No. You can only do it after import but then you need to create the field bigger enough to accept all data.
Alternative is to use SSIS where you can create data transformations.
0
 

Author Comment

by:Omer-Pitou
ID: 40583643
That is what I finally did. I set all the fields to varchar(250).
Ssis was't an option as I wanted it handled programmatically.
Just wonder why MS misses these basic stuffs along with others compared to MySql.
Thanks to all.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now