Dear Sirs,

I have a flat text file that I want to import into a SQL Server table.
The rows don't have delimiters, it is like a long String.
All I know is that: the 5 first characters represent the ID, the next 25 Description, and so no.
How can such an import succeeds with FORMATFILE clause.
An example of code will be appreciated.

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Vitor MontalvãoMSSQL Senior EngineerCommented:
You don't need any code for that.
In SQL Server Management Studio (SSMS) right-click on the database you want to import to and chose Tasks, Import Data... Now just follow the wizard to complete the operation.

Omer-PitouAuthor Commented:
Thanks for your input.
I actually need to include it in code. I can't let the user to go to the database and perform such a risky operation.

SET @Cmd = 'BULK INSERT YourDatabase.dbo.SomeTable FROM ''c:\FilePath\importfile.txt''  WITH (FORMATFILE =''c:\Somewhere\format.fmt'')'

EXEC sp_ExecuteSQL @Cmd

Open in new window


This shows a format file for a flat file with 8 columns of data per line. The
empty delimiter ("") means "use the character length I've provided because
there is no delimiter". The "\r\n" says "start a new row after there's a
carriage return (\r) and a line feed (\n). The "7.0" at the top refers to
a version number so the import knows what sort of format to expect (just
leave it as 7.0 unless you have a good reason to change it). The next line
is how many columns/fields there are in the file: 8. In this example, "Description3" is
being skipped - it will not be imported, so that field should not be in
your database at all.

1 SQLCHAR 0 5      ""            1 ID
2 SQLCHAR 0 25      ""            2 Description
3 SQLCHAR 0 25      ""            3 Description2
4 SQLCHAR 0 25      ""            0 Description3
5 SQLCHAR 0 25      ""            4 Description4
6 SQLCHAR 0 25      ""            5 Description5
7 SQLCHAR 0 25      ""            6 Description6
8 SQLCHAR 0 15 "\r\n"      7 Description7

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.