Solved

MS SQL Bulk Insert

Posted on 2015-02-01
6
180 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 25

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 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

Author Comment

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

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

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

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
I have a large data set and a SSIS package. How can I load this file in multi threading?
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
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

738 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