Tuesday, March 20, 2012

BULK INSERT PROBLEM

I am trying to BULK INSERT a unix data file (.csv) into temperary table, then replace all CHAR(13) into CHAR(10) or '\n'. In Classic ASP, I was using Replace(text,char(13),vbcrlf), but now in MS SQL, so I use Replace(@.text,char(13),'\n').
After finished converting, I will then BULK INSERT into a load_table with rowterminator, firstrow and fieldterminator. But, I got this Error:

============================================

Server: Msg 4866, Level 17, State 66, Line 1
Bulk Insert fails. Column is too long in the data file for row 1, column 1. Make sure the field terminator and row terminator are specified correctly.
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.
OLE DB error trace [OLE/DB Provider 'STREAM' IRowset::GetNextRows returned 0x80004005: The provider did not give any information about the error.].
The statement has been terminated.

==============CODE========================

DECLARE @.sLoadFileLayout AS NVARCHAR(1000)
DECLARE @.SQL AS NVARCHAR(1000)
DECLARE @.sFieldName AS NVARCHAR(1000)
DECLARE @.iRow AS INT
DECLARE @.TextLine AS NVARCHAR(4000)
DECLARE @.fieldTerminator AS char(3)

SET @.sLoadFileLayout = 'C:/folder/file.CSV'


CREATE TABLE #textfile (line varchar(8000))
SET @.SQL = 'BULK INSERT #textfile FROM ''' + @.sLoadFileLayout + ''''
EXEC sp_executesql @.SQL

DECLARE TableMap CURSOR FOR SELECT * FROM #textfile
OPEN TableMap
FETCH NEXT FROM TableMap INTO @.TextLine

SET @.iRow = 0
SET @.SQL = ''
WHILE @.@.FETCH_STATUS = 0
BEGIN
SET @.iRow = @.iRow + 1
IF @.iRow >= 1
BEGIN
IF @.SQL <> '' SET @.SQL = @.SQL + ','
SET @.sFieldName = REPLACE(@.TextLine, char(10),'\n')
SET @.SQL = @.SQL + ' ' + @.sFieldName
END
FETCH NEXT FROM TableMap INTO @.TextLine
END

CLOSE TableMap
DEALLOCATE TableMap

DROP TABLE #textfile

BULK INSERT MARKETING.DBO.LOAD_Table FROM #textfile WITH (DATAFILETYPE='char',FIELDTERMINATOR=',',FIRSTROW=4,ROWTERMINATOR='\n')


Strange. I tested your script and got the same rows in table #textfile and Load_Table. Seems your code works fineSmile I'm using SQL2000, here is the content of the csv file:

One user can only have one user instance.
Replication will be disabled.
User Instance does not support SQL Server Authentication. Only Windows Authentication is supported.
The network protocol support for user instances is local named pipes only.
The user instance shares the registry entries of the parent instance.
There is no support for user instance with native code. This feature is only supported with ADO .NET.
WMI Provider for Server Events will not be supported on the dynamically spawned user instances. This should still work on the parent SQL Server Express instance. For more information on WMI provider see WMI Provider for Server Events in SQL Server 2005 Books Online.

BTW, I think you do not need to replace char(10) or char(13) with '\n', because SQL does accept carriage return.

No comments:

Post a Comment