Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 638
  • Last Modified:

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.
0
LIULIHUA
Asked:
LIULIHUA
  • 2
1 Solution
 
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
 
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now