Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

MS SQL Bulk Insert

Posted on 2015-02-01
6
Medium Priority
?
208 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 25

Assisted Solution

by:chaau
chaau earned 750 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 25

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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 

Author Comment

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

Accepted Solution

by:
Vitor Montalvão earned 750 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

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…
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

963 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