[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 337
  • Last Modified:

SQL Server Fixed Width File Import Issues

Attached is a sample fixed width file of a flie I've downloaded from the FDA site. (I deleted most of the lines because it's huge.) This file, along with others, can be used to create a database of all the OTC & Rx drugs & the companies who registered them.

I haven't imported from a fixed width file before & I'm having problems.

Here is the structure for this file:

ListingSeqNum      Not Null      Num(7)      Col: 1-7      Primary Key
LblCode            Not Null      Char(6)      Col: 9-14
ProdCode      Not Null      Char(4)      Col: 16-19
Strength      Null      Char(10)      Col: 21-30
Unit      Null      Char(10)      Col: 32-41
Rx-OTC      Not Null      Char(1)      Col: 43
Tradename      Not Null      Char(100)      Col: 45-144

Here are my questions:

1. In the text file, there is a single space after every entry. Should I treat that space as a seperate column or as a field terminator?

2. When you look at a fixed width file, is there a way to determine what the field & row terminators are?  LISTINGS-X.TXT
0
LezlyPrime
Asked:
LezlyPrime
  • 2
  • 2
1 Solution
 
wdosanjosCommented:
1. You should treat the space as part of the preceding column. There are no field terminators on fixed file layouts.

Here is how your layout should be:

ListingSeqNum Not Null  Num(8)      Col: 1-8      Primary Key
LblCode       Not Null  Char(7)     Col: 9-15
ProdCode      Not Null  Char(5)     Col: 16-20
Strength      Null      Char(11)    Col: 21-31
Unit          Null      Char(11)    Col: 32-42
Rx-OTC        Not Null  Char(2)     Col: 44
Tradename     Not Null  Char(101)   Col: 45-144

Open in new window


2. Fixed files do not have field delimiters only row delimiters.  Generally row delimiters are the Carriage Return (0xD) and Line Feed (0xA) characters.

I hope this helps.
0
 
LezlyPrimeAuthor Commented:
I followed your advice & extended each of my fields by one. Attached is the design of the Listings table.  395191443.jpg

When I run the following code, I get the following error:

CODE:

 
BULK
INSERT Listings
FROM 'C:\xfer\Rx\LISTINGS.TXT'
WITH
( ROWTERMINATOR = '\r\n'
)
GO

Open in new window

 

ERROR:

Msg 4866, Level 16, State 1, Line 1
The bulk load failed. The column is too long in the data file for row 1, column 1. Verify that the field terminator and row terminator are specified


Any help on what I'm doing wrong would be appreciated.

0
 
wdosanjosCommented:
You are missing the format file reference. Something like:

BULK
INSERT Listings
FROM 'C:\xfer\Rx\LISTINGS.TXT'
WITH
( FORMATFILE ='format_file_path'
)
GO

Open in new window

0
 
LezlyPrimeAuthor Commented:
Thank you!

I didn't know about format files. I queried Experts Exchange & got more info on them & how to create them & got the data inserted.  
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now