Solved

How do I fix this bulk insert

Posted on 2010-09-13
4
728 Views
Last Modified: 2012-05-10
I am getting this error:
The bulk load failed. The column is too long in the data file for row 1, column 3. Verify that the field terminator and row terminator are specified correctly.

I have this proc(Llisted below) to insert into a 3 column table.The text being inserted looks like this:

123456798|12/03/2003|03/19/2004|741258963|12/15/2003|01/14/2004|852147963|12/15/2003|01/14/2004|

Table definition looks like this
ID VARCHAR(9), BEG_BEG, VARCHAR(10), END_DATE VARCHAR(10)

Where am I going wrong?
Thanks
ALTER PROCEDURE dbo.IMPORT_file
	(@DataFileName sysname ='C:\data.TXT'	)
AS
	/* SET NOCOUNT ON */ 

	DECLARE @SQLCommand VARCHAR ( 1012 )
	
	SELECT @SQLCommand = 'Bulk INSERT tmptable FROM ''' + @DataFileName + ''' with ( FIELDTERMINATOR=''|'', ROWTERMINATOR = ''|\n'' ) '			
	PRINT @SQLCommand
	EXEC ( @SQLCommand )	
	
	RETURN

Open in new window

0
Comment
Question by:LIFEEXPERT
  • 2
  • 2
4 Comments
 
LVL 9

Expert Comment

by:valkyrie_nc
ID: 33661879
Your problem is that your file has several rows of data per row, but you've told the Bulk Insert to expect a \n after each row of data. Since it sees more data instead of a \n after the 1st three columns, it throws an error.

You have several options: clean up the file (blech), create an SSIS package and insert that way (by splitting by the columns), or use the attached code (source for the base of the code: http://forums.databasejournal.com/showthread.php?t=47966) to read the file, parse out the 3 pieces of each row's data in the rows, and insert that way.

hth

valkyrie_nc
CREATE PROCEDURE ProcessTextFile (@FileName VARCHAR (1024), @delimiterChar char(1))

AS

DECLARE @OLEResult INT
DECLARE @FS INT
DECLARE @FileID INT
DECLARE @Message VARCHAR (8000)

-- Create an instance of the file system object
EXECUTE @OLEResult = sp_OACreate 'Scripting.FileSystemObject', @FS OUT
IF @OLEResult <> 0
BEGIN
PRINT 'Scripting.FileSystemObject'
PRINT 'Error code: ' + CONVERT (VARCHAR, @OLEResult)
END

-- Open the text file for reading
EXEC @OLEResult = sp_OAMethod @FS, 'OpenTextFile', @FileID OUT, @FileName, 1, 1
IF @OLEResult <> 0
BEGIN
PRINT 'OpenTextFile'
PRINT 'Error code: ' + CONVERT (VARCHAR, @OLEResult)
END

-- Read the first line into the @Message variable
EXECUTE @OLEResult = sp_OAMethod @FileID, 'ReadLine', @Message OUT

-- Keep looping through until the @OLEResult variable is < 0; this indicates that the end of the file has been reached.
WHILE @OLEResult >= 0
BEGIN

-- Loop for every 3 sets of data
	DECLARE @pos int
	DECLARE @piece varchar(50)
	DECLARE @ID varchar(9)
	DECLARE @BEG_DATE varchar(10)
	DECLARE @END_DATE varchar(10)
	DECLARE @counter int
	
	SET @counter = 0
	
	IF RIGHT(RTRIM(@Message), 1) <> @delimiterChar
		SET @Message = @Message + @delimiterChar
	
	SET @pos = PATINDEX('%' + @delimiterChar + '%',@Message)
	WHILE @pos <> 0
	BEGIN
		SET @piece = LEFT(@Message, @pos - 1)
		IF @counter = 0
		BEGIN
			SET @ID = @piece
			SET @counter = @counter + 1
		END
		ELSE IF @counter = 1
		BEGIN
			SET @BEG_DATE = @piece
			SET @counter = @counter + 1
		END
		ELSE IF @counter = 2
		BEGIN
			SET @END_DATE = @piece
			SET @counter = 0
			INSERT INTO tmptable (ID, BEG_DATE, END_DATE) values (@ID, @BEG_DATE, @END_DATE)
		END
		
		SET @Message = STUFF(@Message, 1, @pos, '')
		SET @pos = PATINDEX('%' + @delimiterChar + '%', @Message)
	END

	EXECUTE @OLEResult = sp_OAMethod @FileID, 'ReadLine', @Message OUT
END

EXECUTE @OLEResult = sp_OADestroy @FileID
EXECUTE @OLEResult = sp_OADestroy @FS

GO

Open in new window

0
 

Author Comment

by:LIFEEXPERT
ID: 33661947
I thought the same thing..."Your problem is that your file has several rows of data per row", but when I cut and past this into MSword it puts the line return in and divides the data into correct rows; as if I just couldn't see it in Notepad. Any idea why?
0
 
LVL 9

Expert Comment

by:valkyrie_nc
ID: 33662017
Hmmm.  Maybe it wants the old-school terminator "\r\n"?
0
 

Accepted Solution

by:
LIFEEXPERT earned 0 total points
ID: 33662047
I tried that. Thanks for your solution. I figured out an alternative, that is much simpler. For future reference to all...

SELECT @SQLCommand = 'Bulk INSERT tmptable  FROM ''' + @DataFileName + ''' with ( FIELDTERMINATOR=''|'', ROWTERMINATOR = ''|' + nchar(10) + ''')'      
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
I am showing a way to read/import the excel data in table using SQL server 2005... Suppose there is an Excel file "Book1" at location "C:\temp" with column "First Name" and "Last Name". Now to import this Excel data into the table, we will use…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

813 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now