Maria Torres
asked on
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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:
Would someone know how to resolve this error? Also, what does it mean?
Thank you.
=======
(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_
=========
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
Would someone know how to resolve this error? Also, what does it mean?
Thank you.
Can you show us part of your input file? stnjep16.dat
ASKER
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.
Thanks you all for your input.
de nada Carmen!
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,