How to write a control file in T-SQL (similar to Oracle' s control file for loading data)?

I have control files that we use to upload data onto Oracle 9i server.  I need to rewrite these control files to T-SQL (we will be migrating to a SQL-Server). I've searched the web but haven't found any articles on creating control files in T-SQL.  

I've seen many articles on BULK INSERT but it does not apply to my situation.  The control files that we have do not include all the fields that the table has; also, the flat file containing the data to be loaded does not have the fields in the corresponding order as of table.

Can anyone point me in the right direction on writing a script file that will allow me to load the data in a specific order that is not based on the tables' field order?

Thank you
Maria TorresData AnalystAsked:
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.

Aneesh RetnakaranDatabase AdministratorCommented:
If you are using bcp / bulk insert, your only option is to use the Format File, i'm not sure why this option wont work for you.
Otherwise you can create SSIS packages to copy the data from the flat file to appropriate columns.
Next option is to create linked server, and copy the necessary data using the insert statement, this way you can control the columns order,
What you will need is a FORMAT FILE , that would be the equivalent of oracle CONTROL FILES.
here are 2 links from MS that explain in detail how to achieve this:


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
Maria TorresData AnalystAuthor Commented:
I created a format file, via BCP.  When I try to load the data using the format file, I get the following message:
(0 row(s) affected)
Msg 4862, Level 16, State 1, Line 6
Cannot bulk load because the file "S:\CARMEN\fmtEspd.fmt" is incomplete or could not be read. Operating system error code (null).

I then modified the format file to only contain one record (column), to see if I can at least populate one field;  I still get the above error.

My format file is as follows:
1       SQLCHAR             1       5       "~"      1     CNVRSN_SRC_CD                              SQL_Latin1_General_CP1_CI_AS

My script file that performs the bulk insert is as follows:
   FROM 'S:\CARMEN\stnjep16.dat'   
   WITH (FORMATFILE ='S:\CARMEN\fmtEspd.fmt');  

Open in new window

Would someone know how to resolve this error?  Also, what does it mean?

Thank you.
Acronis True Image 2019 just released!

Create a reliable backup. Make sure you always have dependable copies of your data so you can restore your entire system or individual files.

Can you show us part of your input file? stnjep16.dat
Maria TorresData AnalystAuthor Commented:
I determined that my bcp syntax was incorrect; I omitted the keyword "IN".  Once I corrected the syntax, I was able to populate the table.

Thanks you all for your input.
de nada Carmen!
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.