Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Import/Export (DTS) and Replication (2000)
 Truncation error using Bulk Insert

Author  Topic 

Satiex
Starting Member

7 Posts

Posted - 2007-06-06 : 12:59:52
Hi, I'm having trouble using Bulk Insert to import data from a .txt file into a table in Microsoft SQL Server.

Table Structure:

CREATE TABLE Member
(MemberID BigInt Identity(10001,1) Primary Key Not Null,
Fname Char(20) Not Null,
Lname Char(20) Not Null,
DOB Datetime Not Null,
Street Char(30) Not Null,
Suburb Char(20) Not Null,
State Char(3) Not Null,
Pcode Char(4) Not Null,
Phone Char(12),
Mobile Char(11),
DLicNo Char(6 ),
JoinDate Datetime Not Null,
AccBalance Dec(6,2) Not Null,
ValidMember Int Not Null,
Passwd Char(6 ) Not Null,
Constraint CC_ValidMember CHECK (ValidMember IN ('0', '1', '2')))
GO


These are the first few rows from the .txt file:

"MemberID","Fname","Lname","DOB","Street","Suburb","State","Pcode","Phone","Mobile","DLicNo","JoinDate","AccBalance","ValidMember","Passwd"
100001,"John","News",7/6/1970 0:00:00,"123 Oak St.","Sydney","NSW",2000,"(02)97451165","0413-111111","91231C",1/5/1997 0:00:00,0,1,"aaaaaa"
100002,"David","Senior",8/1/1940 0:00:00,"18 Belmorw St.","Bankstown","NSW",2115,"(02)97411325",,"45231C",1/5/1997 0:00:00,0,1,"aaaaab"
100003,"Ann","Parsons",12/3/1981 0:00:00,"24 Surf St.","Manly","NSW",2095,"(02)99415356",,"57129C",14/6/1997 0:00:00,0,1,"penguin"


This is my Script:

USE Megavideos
GO
BULK INSERT Megavideos.dbo.Member
FROM 'C:\Database\Member.txt'
With
(
firstrow = 2,
fieldterminator = ','
)


These are the last few lines of the error messages I'm getting:

Server: Msg 4863, Level 16, State 1, Line 1
Bulk insert data conversion error (truncation) for row 12, column 7 (State).
Server: Msg 4865, Level 16, State 1, Line 1
Could not bulk insert because the maximum number of errors (10) was exceeded.
Server: Msg 7399, Level 16, State 1, Line 1
OLE DB provider 'STREAM' reported an error. The provider did not give any information about the error.
The statement has been terminated.


The problem seems to be with column 7 (State) but I'm not sure what the probem is. Any help would be appreciated.

Thanks

sshelper
Posting Yak Master

216 Posts

Posted - 2007-06-06 : 13:12:23
Based on the error message that you received, check your text file, particularly row 12, and see the value for the State. Most probably it's more than 3 characters.

SQL Server Helper
http://www.sql-server-helper.com
Go to Top of Page

dinakar
Master Smack Fu Yak Hacker

2507 Posts

Posted - 2007-06-06 : 13:16:11
Also, its better to use varchar's instead of chars. You could save some space besides your queries returning incorrect results.

Dinakar Nethi
************************
Life is short. Enjoy it.
************************
http://weblogs.sqlteam.com/dinakar/
Go to Top of Page

Satiex
Starting Member

7 Posts

Posted - 2007-06-06 : 13:18:36
Thanks I just worked it out. In the text file, all Char datatypes were enclosed in "", seperated by a ,
This caused the "" to be included in the value which truncated the width of the column. Silly me.

Thanks for the quick reply =)
Go to Top of Page

Satiex
Starting Member

7 Posts

Posted - 2007-06-06 : 22:12:22
I am now getting something quite bizare.

I have saved a query script which contains all my queries (Create the database, create the tables, import the data into the tables) and when I run each script they work fine.

However, I created 3 seperate query files, one each for creating the database, tables and importing the data. I open up a script, run it, then close it and open up the next script.
where I open the Import Data script (the Bulk Insert script) it gives me a few errors

Server: Msg 4864, Level 16, State 1, Line 1
Bulk insert data conversion error (type mismatch) for row 4, column 12 (JoinDate).
Server: Msg 4864, Level 16, State 1, Line 1
Bulk insert data conversion error (type mismatch) for row 5, column 12 (JoinDate).
Server: Msg 4864, Level 16, State 1, Line 1
Bulk insert data conversion error (type mismatch) for row 9, column 12 (JoinDate).


It gives me errors for the DOB column aswell, and many errors for various rows of other tables.

If it's all working when I have it all in one query file, is there any apparent reason seperating the script into
3 seperate scripts shouldn't work?
Go to Top of Page

rmiao
Master Smack Fu Yak Hacker

7266 Posts

Posted - 2007-06-08 : 01:33:04
Tried put double quote around date value?
Go to Top of Page

Satiex
Starting Member

7 Posts

Posted - 2007-06-08 : 05:00:13
I got it to work. In the script for creating the tables I included the line "SET DATEFORMAT DMY" which takes effect over the whole script.

I included the SET DATEFORMAT DMY in the Import Data script and it works fine =)
Go to Top of Page
   

- Advertisement -