简体   繁体   中英

Bulk insert to table

I have next table:

CREATE TABLE [dbo].[tempTable](
    [id] [varchar](50) NULL,
    [amount] [varchar](50) NULL,
    [bdate] [varchar](50) NULL
)

and next insert statement:

BULK INSERT dbo.tempTable
   FROM 'C:\files\inv123.txt'
   WITH 
      (
         FIELDTERMINATOR ='\t',
         ROWTERMINATOR ='\n'
      )

I get next error:

Bulk load data conversion error (truncation) for row 1, column 3 (bdate).

Data example in file:

12313 24 2012-06-08 13:25:49
12314 26 2012-06-08 12:25:49

It does look like it is just not ever delimiting the row. I've had to separate rows by column delimiter AND row delimiter because the text file had a post ceding (and unnecessary) column delimiter after the last value that it took me awhile to spot. Those dates would certainly fit the format (assuming there just isn't some bad data in a huge file you can't visually spot, and since it doesn't fail until 10 errors by default there'd be at least that many bad records) and it looks like it is making it to that point correctly. View the file in hex in a good text editor if you can and see or just try:

BULK INSERT dbo.tempTable
FROM 'C:\files\inv123.txt'
WITH 
  (
     FIELDTERMINATOR ='\t',
     ROWTERMINATOR = '\t\n' 
  )

Another possibility (that I doubt considering it is varchar(50) ) is that there are headers in the inv123.txt file and the header is being perceived as a row and is exceeding varchar(50) and it is what is being truncated. In this case you can add

FIRSTROW = 2, 

If it still fails after these things, try to force some data in or grab the rows that are errorring so you'll truly know where the problem is. Look into set ansi_warnings off or using ERRORFILE depending on what flavor SQL SERVER or create a temp table with text as the datatype. SQL Server 2005 forces stricter data validation and forcing an insert without fail is more difficult but can be done.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM