Solved

MS SQL Bulk Insert

Posted on 2015-02-01
6
166 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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 

Author Comment

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

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

773 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