This article describes how to use the bcp utility to create a format file for a particular table. The format file is based on the data-type option specified (-n, -c, -w, or -N) and the table or view delimiters.
When you bulk import into a SQL Server table or bulk export data from a table, you can use a format file as a flexible system for writing data files. Format files require little or no editing to comply with other data formats, or to read data files from other software programs.
Limitations
The version of the bcp utility (bcp.exe) used to read a format file must be the same as, or later than the version used to create the format file. For example, SQL Server 2016 (13.x) bcp can read a version 12.0 format file, which is generated by SQL Server 2014 (12.x) bcp, but SQL Server 2014 (12.x) bcp can't read a version 13.0 format file, which is generated by SQL Server 2016 (13.x) bcp.
SQL Server support two types of format file: non-XML format and XML format. The non-XML format is the original format supported by earlier versions of SQL Server.
Generally, XML and non-XML format files are interchangeable. However, we recommend that you use XML syntax for format files, because they provide several advantages over non-XML format files.
The code samples in this article use the AdventureWorks2022 or AdventureWorksDW2022 sample database, which you can download from the Microsoft SQL Server Samples and Community Projects home page. Adventure Works Cycles is a fictional manufacturing company used to demonstrate database concepts and scenarios.
Create an XML format file
To use a bcp command to create a format file, specify the format argument and use nul instead of a data-file path. The format option always requires the -f option, and to create an XML format file, you must also specify the -x option, such as bcp <table_or_view> format nul -f <format_file_name> -x.
To distinguish an XML format file, we recommend that you use .xml as the file name extension, for example, MyTable.xml.
This section contains the following examples that show how to use bcp commands to create an XML format file. The HumanResources.Department table contains four columns: DepartmentID, Name, GroupName, and ModifiedDate.
A. Create an XML format file for character data
The following example creates an XML format file, Department.xml, for the HumanResources.Department table. The format file uses character data formats and a non-default field terminator (,). The contents of the generated format file are presented after the command.
The bcp command contains the following qualifiers.
Qualifiers
Description
format nul -x -f <format_file>
Specifies the XML format file.
-c
Specifies character data.
-t,
Specifies a comma (,) as the field terminator.
Note: If the data file uses the default field terminator (\t), the -t switch is unnecessary.
-T
Specifies that the bcp utility connects to SQL Server with a trusted connection using integrated security. If -T isn't specified, you must specify -U and -P to successfully sign in.
At the Windows command prompt, enter the following bcp command:
bcp AdventureWorks2022.HumanResources.Department format nul -c -x -f Department-c.xml -t, -T
The generated format file, Department-c.xml, contains the following XML elements:
The following example creates an XML format file, Department-n.xml, for the HumanResources.Department table. The format file uses native data types. The contents of the generated format file are presented after the command.
The bcp command contains the following qualifiers.
Qualifiers
Description
format nul -x -f <format_file>
Specifies the XML format file.
-n
Specifies native data types.
-T
Specifies that the bcp utility connects to SQL Server with a trusted connection using integrated security. If -T isn't specified, you must specify -U and -P to successfully sign in.
At the Windows command prompt, enter the following bcp command:
bcp AdventureWorks2022.HumanResources.Department format nul -x -f Department-n.xml -n -T
The generated format file, Department-n.xml, contains the following XML elements:
The code samples in this article use the AdventureWorks2022 or AdventureWorksDW2022 sample database, which you can download from the Microsoft SQL Server Samples and Community Projects home page. Adventure Works Cycles is a fictional manufacturing company used to demonstrate database concepts and scenarios.
Create a non-XML format file
To use a bcp command to create a format file, specify the format argument and use nul instead of a data-file path. The format option also requires the -f option, such as: bcp <table_or_view> format nul -f <format_file_name>.
To distinguish a non-XML format file, we recommend that you use .fmt as the file name extension, for example, MyTable.fmt.
This section contains the following examples that show how to use bcp commands to create a non-XML format file. The HumanResources.Department table contains four columns: DepartmentID, Name, GroupName, and ModifiedDate.
A. Create a non-XML format file for native data
The following example creates an XML format file, Department-n.xml, for the HumanResources.Department table. The format file uses native data types. The contents of the generated format file are presented after the command.
The bcp command contains the following qualifiers.
Qualifiers
Description
format nul -f <format_file>
Specifies the non-XML format file.
-n
Specifies native data types.
-T
Specifies that the bcp utility connects to SQL Server with a trusted connection using integrated security. If -T isn't specified, you must specify -U and -P to successfully sign in.
At the Windows command prompt, enter the following bcp command:
bcp AdventureWorks2022.HumanResources.Department format nul -T -n -f Department-n.fmt
The generated format file, Department-n.fmt, contains the following information:
B. Create a non-XML format file for character data
The following example creates an XML format file, Department.fmt, for the HumanResources.Department table. The format file uses character data formats and a non-default field terminator (,). The contents of the generated format file are presented after the command.
The bcp command contains the following qualifiers.
Qualifiers
Description
format nul -f <format_file>
Specifies a non-XML format file.
-c
Specifies character data.
-T
Specifies that the bcp utility connects to SQL Server with a trusted connection using integrated security. If -T isn't specified, you must specify -U and -P to successfully sign in.
At the Windows command prompt, enter the following bcp command:
bcp AdventureWorks2022.HumanResources.Department format nul -c -f Department-c.fmt -T
The generated format file, Department-c.fmt, contains the following information:
D. Create a non-XML format file for Unicode character data
To create a non-XML format file for Unicode character data for the HumanResources.Department table that uses default terminators, use the following command:
bcp AdventureWorks2022.HumanResources.Department format nul -T -w -f Department-w.fmt
If you try to import data into SQL Server using bcp in -c -C65001 -f format_file ..." or "BULK INSERT/OPENROWSET ... FORMATFILE='format_file' CODEPAGE=65001 ...", information about the collation/code page has priority over 65001 option.
Therefore, if you generate a format file, you must manually delete the collation info from the generated format file before you start importing data back into SQL Server.
The following example shows the format file without the collation info.
As created by bcp, a format file describes all the table columns in order. You can modify a format file to rearrange or omit table rows. You can customize a format file to a data file whose fields don't map directly to the table columns. For more information, see the following articles:
Do you want to know how to create new tables in Business Central? If so, this module is for you. This module focuses on the different table types in Business Central and show you how to create new tables. Additionally, you learn how to use Visual Studio Code snippets to create a table, fields, and keys.