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?
 
aranaConnect With a Mentor Commented:
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:

0
 
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,
0
 
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:
=========
12.0
1
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:
USE HCDA;  
GO  
DELETE dbo.ST_NJ_EPSD_16;  
GO  
BULK INSERT dbo.ST_NJ_EPSD_16   
   FROM 'S:\CARMEN\stnjep16.dat'   
   WITH (FORMATFILE ='S:\CARMEN\fmtEspd.fmt');  
GO  
SELECT * FROM dbo.ST_NJ_EPSD_16;  
GO 

Open in new window


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

Thank you.
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
aranaCommented:
Can you show us part of your input file? stnjep16.dat
0
 
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.
1
 
aranaCommented:
de nada Carmen!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.