Quantcast
Channel: SQLServerCentral » SQL Server 2014 » Development - SQL Server 2014 » Latest topics
Viewing all articles
Browse latest Browse all 3145

Bulk insert a fixed width file

$
0
0
I wasn't sure where to put this topic so I put it here since I figured it is a question that would apply to virtually any version even though I am using SQL Server 2005.We have a vendor that sends us a fixed width text file every day that needs to be imported to our database in 3 different tables. I am trying to import all of the data to a staging table and then plan on merging/inserting select data from the staging table to the 3 tables. The file has 77 columns of data and 20,000+ records. I created an XML format file which I sampled below:[code="xml"]<?xml version="1.0"?><BCPFORMAT xmlns="http://schemas.microsoft.com/sqlserver/2004/bulkload/format" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><RECORD> <FIELD ID="RetNo" xsi:type="CharFixed" LENGTH="6"/> <FIELD ID="TrName" xsi:type="CharFixed" LENGTH="30"/> <FIELD ID="Add1" xsi:type="CharFixed" LENGTH="30"/></RECORD><ROW> <COLUMN SOURCE="RetNo" NAME="AGENT_NUMBER" xsi:type="SQLCHAR" LENGTH="6"/> <COLUMN SOURCE="TrName" NAME="TRADE_NAME" xsi:type="SQLCHAR" LENGTH="30"/> <COLUMN SOURCE="Add1" NAME="ACTUAL_NO_STREET" xsi:type="SQLCHAR" LENGTH="30"/> <COLUMN SOURCE="Add2" NAME="ADDRESS2" xsi:type="SQLCHAR" LENGTH="30"/></ROW></BCPFORMAT>[/code]The data file is a fixed width file with no column delimiters or row delimiters that I can tell. When I run the following insert statement I get the error below it.[code="sql"]BULK INSERT myStagingTable FROM '\\...\...\...\myDataSource.txt' WITH ( FORMATFILE = '\\...\...\...\myFormatFile.xml', ERRORFILE = '\\...\...\...\errorlog.log' );[/code]Here is the error:[quote]Msg 4832, Level 16, State 1, Line 1Bulk load: An unexpected end of file was encountered in the data file.Msg 7399, Level 16, State 1, Line 1The OLE DB provider "BULK" for linked server "(null)" reported an error. The provider did not give any information about the error.Msg 7330, Level 16, State 2, Line 1Cannot fetch a row from OLE DB provider "BULK" for linked server "(null)".[/quote]Any suggestions would be greatly appreciated. I would like to just ask the vendor for a different format but that would likely take too long.

Viewing all articles
Browse latest Browse all 3145

Trending Articles