Solved

How do I fix this bulk insert

Posted on 2010-09-13
4
725 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
Comment Utility
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
Comment Utility
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
Comment Utility
Hmmm.  Maybe it wants the old-school terminator "\r\n"?
0
 

Accepted Solution

by:
LIFEEXPERT earned 0 total points
Comment Utility
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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Join & Write a Comment

If you having speed problem in loading SQL Server Management Studio, try to uncheck these options in your internet browser (IE -> Internet Options / Advanced / Security):    . Check for publisher's certificate revocation    . Check for server ce…
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

771 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