.... oh just realised that SP is for importing CSV files. It will need to be changed to import fixed width.
Main Topics
Browse All TopicsI have about 50 separate fixed width text files that I need to pull into SQL Server 2000, so I want to automate the process. They are BIG files, with 731 fields and a sum of 250 gigs.
I have a list of the field names and lengths, so I want to use that to instruct the transfer. I already have created a template table in SQL Server with the corresponding fields so I only need to append the records.
I would love to do this in VBA using an Access project (.adp) with stored procedures if that can be done. But as long as there is a not-too-crazy solution to this, that would be great. I've been looking at the questions on this and there are bulk inserts but I'm only getting more confused.
Thanks.
The table with field lengths looks like this with field name and length:
F1 18
F2 15
F3 1
F4 20
F5 2
etc........
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
you will need to have a format file, which you can use along with the BULK INSERT, which specifies the columns and their lengths
"To import data from data files with fixed-length or fixed-width fields, you must use a format file"
Preparing Data for Bulk Export or Import
http://msdn2.microsoft.com
Thanks. I am feeling very slow. I would love a basic description of how to set this up. Am I creating a couple of stored procedures, and is the bulk insert a command for that.? I see what I think is a format for defining the fixed width tables (copied below).
I could use a short, sample code on this, like an actual SP. I'm not a SQL Server person, but rather a general developer that uses SQL Server for programming db apps.
Thanks.
<?xml version="1.0"?>
<BCPFORMAT
xmlns="http://schemas.micr
xmlns:xsi="http://www.w3.o
<RECORD>
<FIELD ID="1" xsi:type="CharFixed" LENGTH="10"/>
<FIELD ID="2" xsi:type="CharFixed" LENGTH="6"/>
<FIELD ID="3" xsi:type="CharTerm" TERMINATOR="\r\n"
</RECORD>
<ROW>
<COLUMN SOURCE="1" NAME="C1" xsi:type="SQLINT" />
<COLUMN SOURCE="2" NAME="C2" xsi:type="SQLINT" />
</ROW>
</BCPFORMAT>
you need to create a .fmt Format file and reference that file in the BULK INSERT statement thus:
BULK INSERT myTbl
FROM 'c:\myFile.txt'
WITH (FORMATFILE='c:\FormatFile
see these links for info on creating a fmt file:
Using the bcp Format File
http://doc.ddart.net/mssql
Creating a Format File
http://msdn2.microsoft.com
Ok, just to be clear, since I'm confused about the difference between bcp and bulk insert. The "creating a format file" directions is about creating a bcp format file (see below). But I can creat an xml type file as below and name it fileformat.fmt and that will be the file I use for your simple bulk insert command you have above?
Sorry for my slowness on this......
<?xml version="1.0"?>
<BCPFORMAT
xmlns="http://schemas.micr
xmlns:xsi="http://www.w3.o
<RECORD>
<FIELD ID="1" xsi:type="CharTerm" TERMINATOR="\t"
MAX_LENGTH="12"/>
<FIELD ID="2" xsi:type="CharTerm" TERMINATOR="\t"
MAX_LENGTH="20" COLLATION="SQL_Latin1_Gene
<FIELD ID="3" xsi:type="CharTerm" TERMINATOR="\r\n"
MAX_LENGTH="30"
COLLATION="SQL_Latin1_Gene
</RECORD>
<ROW>
<COLUMN SOURCE="1" NAME="age" xsi:type="SQLINT"/>
<COLUMN SOURCE="2" NAME="firstname" xsi:type="SQLVARYCHAR"/>
<COLUMN SOURCE="3" NAME="lastname" xsi:type="SQLVARYCHAR"/>
</ROW>
</BCPFORMAT>
I'm not aware of a format file ever being XML. All the format files I have used are straight text files.
If you look up format files in books online, and choose the first entry, you'll find this example of a format file:
8.0
9
1 SQLCHAR 0 11 "," 1 au_id SQL_Latin1_General_Cp437_B
2 SQLCHAR 0 40 "," 2 au_lname SQL_Latin1_General_Cp437_B
3 SQLCHAR 0 20 "," 3 au_fname SQL_Latin1_General_Cp437_B
4 SQLCHAR 0 12 "," 4 phone SQL_Latin1_General_Cp437_B
5 SQLCHAR 0 0 "" 5 address SQL_Latin1_General_Cp437_B
6 SQLCHAR 0 20 "," 6 city SQL_Latin1_General_Cp437_B
7 SQLCHAR 0 2 "," 7 state SQL_Latin1_General_Cp437_B
8 SQLCHAR 0 0 "" 8 zip SQL_Latin1_General_Cp437_B
9 SQLCHAR 0 1 "\r\n" 9 contract SQL_Latin1_General_Cp437_B
Thats the format you are aiming for. The format file tells BULK INSERT or BCP.EXE where each fixed width field sits, and what data type it is.
This is in SQL 2000 right?
BULK INSERT and BCP are almost exactly the same.
However BULK INSERT is a T-SQL command - you submit it over a database connection
BCP.EXE is an EXE, run from the command line.
They both use exactly the same kind of format file though.
Once you have successfuly manually imported one of your files using BULK INSERT, you can either append them all together and insert in one go, or use the link I provided for a SP that will loop through a folder and BULK INSERT the files.
Thanks nmcdermaid for the detailed explanation.
I did finally do more reading on it and figured out to use the text format. The MS documentation really is not as robust as I would like. I had one head banging moment where I couldn't get it to work and it turned out that I didn't put a CR at the end of the last line of the text file. Somebody on some user group mentioned that and that was the problem.
I'm still having issues getting it in correctly. It's hard because they are HUGE files with 731 fields and a total of 130 million records. I'm going to break it up though.
Thanks again.
Business Accounts
Answer for Membership
by: nmcdermaidPosted on 2007-09-02 at 18:33:03ID: 19818649
See here for a stored procedure that will BULK insert all files into a table:
e.com/Micr osoft/Deve lopment/MS -SQL- Serve r/Q_217006 52.html
http://www.experts-exchang
Note that you will need to set up a format file and test it first. This is really only useful it you want to automate the import.
If its a one off then you could append all of the files together into a couple of larger files using
COPY File1.TXT + File2.TXT + File3.TXT OutputFile.TXT
and just bulk insert OutputFile.TXT in one go.