Save a table formate as .fmt file

Hi experts,

Here is the case, I have few tables in the SQL database instance, I am going to load text files to the table using Bulk insert task. The problem is I don't have .fmt file created for each of the table's formate. How can I do that?

Thanks.
LIULIHUAAsked:
Who is Participating?
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:
follow this example


Format file structure (see bol)

8.0                            version
1                            number of columns
1 SQLCHAR 0 1 "\r\n" 1 col1 Latin1_General_CI_AS    col number, file data type, prefix length, data length, terminator, table col order, table col name, file collation

Note - can use version 7.0 and omit the collation.

Simple import using format file

-- create the bcp file
a
b
c

-- create the destination table
create table ##a (col1 varchar(10))

-- create the format file
8.0
1
1 SQLCHAR 0 0 "\r\n" 1 col1 Latin1_General_CI_AS

master..xp_cmdshell 'bcp ##a in c:\bcp.txt -fc:\bcpfmt.txt'
bulk insert ##a from 'c:\bcp.txt' with (formatfile = 'c:\bcpfmt.txt')

Import with multiple columns
-- create the bcp file
1,a,b,c
2,d,e,f
3,g,h,i

-- create the destination table
create table ##a (i int, s varchar(2), t varchar(2), u varchar(2))

-- create the format file
8.0
4
1 SQLCHAR 0 0 "," 1 i Latin1_General_CI_AS
2 SQLCHAR 0 0 "," 2 s Latin1_General_CI_AS
3 SQLCHAR 0 0 "," 3 t Latin1_General_CI_AS
4 SQLCHAR 0 0 "\r\n" 4 u Latin1_General_CI_AS

master..xp_cmdshell 'bcp ##a in c:\bcp.txt -fc:\bcpfmt.txt'
bulk insert ##a from 'c:\bcp.txt' with (formatfile = 'c:\bcpfmt.txt')
0

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
nmcdermaidCommented:
If the fields in the source file match the fields in the destination table, in number, order and data type, then you don't need a format file:


BULK INSERT Dataload_SC_GroupedRating
FROM 'YourSourceFile.CSV'
WITH
(
FIELDTERMINATOR=','
)
0
LIULIHUAAuthor Commented:
a. the fields in the source file dosen't match all the fields in the destination table
b. the delimiter of fileds is using "~" instead of " or '.
c. some table has more then 30 fileds. Take one of example as following:
d. This is the first time I use DTS to load data, would you please give the details of how to implement Bulk insert task. I appreciate!



CREATE TABLE [ACCOUNT] (
      [ACCOUNT_ID] [varchar] (12) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
      [ACCOUNT_ID PIV] [varchar] (12) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
      [COMPANY_NAME] [varchar] (64) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
      [SUPPRESS] [char] (1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
      [ADDRESS1] [nvarchar] (40) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
      [CITY] [nvarchar] (30) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
      [STATE] [nvarchar] (2) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
      [ZIP] [nvarchar] (7) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
      ........
0
nmcdermaidCommented:
If the fields don't match then use aneeshattingal's solution.
0
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.

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.