Solved

BULK INSERT WITH FORMATFILE

Posted on 2004-08-23
3
1,228 Views
Last Modified: 2007-12-19
Hi Experts,

I want to insert records from a Comma seperated text file. I want to use FORMATFILE  to ignore Salary Field.

BULK INSERT TestEmp
From 'D:\Code\Refresh\New\SQLScripts\Empltest.txt'
  WITH(FIRSTROW = 2, FORMATFILE = 'd:\Code\Refresh\New\SQLScripts\Empl.fmt')

I am getting the following error:

"Bulk Insert fails. Column is too long in the data file for row 1, column 3. Make sure the field terminator and row terminator are specified correctly."


FORMATFILE - Empl.fmt
===============
8.0
5
1       SQLCHAR       0       10      ","                       1     EmpCode      SQL_Latin1_General_CP1_CI_AS
2       SQLCHAR       0       50      ","                       2     EmpName      SQL_Latin1_General_CP1_CI_AS
3       SQLDATETIME   1       4       ","                       3     DOJ          ""
4       SQLMONEY      1       8       ","                       0     Salary       ""
5       SQLCHAR       0       0       "\r\n"                    5     Comments     SQL_Latin1_General_CP1_CI_AS
-----------------------------------------------------------

TABLE - TESTEMP
===========
CREATE TABLE [TestEmp] (
      [EmpCode] [char] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
      [EmpName] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
      [DOJ] [smalldatetime] NULL ,
      [Salary] [money] NULL ,
      [Comments] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
) ON [PRIMARY]
GO

TEXTFILE - EMPLTEST.TXT
================

"EmpCode","EmpName","DOJ","Salary","Comments"
"001","Empl-A","10/01/2004","50000.00","AA"
"002","Empl-B","12/01/2004","45000.00","CC"
"003","Empl-C","05/25/2004","25000.00","DD"
"004","Empl-D","06/30/2004","35000.00","EE"
"005","Empl-E","02/28/2004","60000.00","FF"
-----------------------------------

Can you tell me, what's wrong with this formatfile?

Thanks in advance,
sps
0
Comment
Question by:sps2004
3 Comments
 
LVL 19

Accepted Solution

by:
Melih SARICA earned 125 total points
ID: 11878071
FORMATFILE - Empl.fmt
===============
8.0
5
1       SQLCHAR       0       10      ","                       1     EmpCode      SQL_Latin1_General_CP1_CI_AS
2       SQLCHAR       0       50      ","                       2     EmpName      SQL_Latin1_General_CP1_CI_AS
3       SQLDATETIME   1      8      ","                       3     DOJ          ""
4       SQLMONEY      1       8       ","                       0     Salary       ""
5       SQLCHAR       0       0       "\r\n"                    5     Comments     SQL_Latin1_General_CP1_CI_AS
-----------------------------------------------------------

USe This Format file


Why  ? cuz SQL BOL says that :
"
Note  Using default data type sizes (field length) can lead to an "unexpected end of file" error message. This generally occurs with the money and datetime data types when only part of the field occurs in the data file (for example, a datetime value of mm/dd/yy with no time component) rather than an entire string, as expected by SQL Server. When using the default size option, SQL Server expects to read 24 characters (the length of the datetime data type when stored in char format). To avoid this problem, bulk copy data using field terminators, or fixed-length data fields.


Specifying a field length too short for numeric data when bulk copying data causes bcp to print an overflow message and not copy the data. When datetime data is copied to a data file as a character string of less than 26 bytes, the data is truncated without an error message. When creating an ASCII data file, use the default field length to ensure that data is not truncated and that numeric overflow errors causing bcp to fail do not occur. To change the default field length, supply another value.


"
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
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.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

756 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