
Creating 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
Note: |
|---|
|
To distinguish an XML format file, we recommend that you use .xml as the file name extension, for example, MyTable.xml.
|
For information about the structure and fields of XML format files, see Understanding XML Format Files. For syntax information, see Schema Syntax for XML Format Files. For examples, see Sample XML Format Files.
Examples
This section contains the following examples that show how to use bcp commands to create an XML format file:
-
A. Creating an XML format file for character data
-
B. Creating an XML format file for native data
The examples use the HumanResources.Department table in the AdventureWorks sample database. The HumanResources.Department table contains four columns: DepartmentID, Name, GroupName, and ModifiedDate. For the definition of this table, see Department Table (AdventureWorks).
Note: |
|---|
|
Adventure Works Cycles is a fictional manufacturing company used to demonstrate database concepts and scenarios.
|
A. Creating an XML format file for character data
The following example creates an XML format file, Department.xml, for the AdventureWorks 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 -x
|
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 is not specified, you must specify -U and -P to successfully log in.
|
At the Windows command prompt, enter the following bcp command:
bcp AdventureWorks.HumanResources.Department format nul -c -x -f Department-c..xml –t, -T
The generated format file, Department-c.xml, contains the following XML elements:
<?xml version="1.0"?>
<BCPFORMAT xmlns="http://schemas.microsoft.com/sqlserver/2004/bulkload/format" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<RECORD>
<FIELD ID="1" xsi:type="CharTerm" TERMINATOR="," MAX_LENGTH="7"/>
<FIELD ID="2" xsi:type="CharTerm" TERMINATOR="," MAX_LENGTH="100" COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
<FIELD ID="3" xsi:type="CharTerm" TERMINATOR="," MAX_LENGTH="100" COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
<FIELD ID="4" xsi:type="CharTerm" TERMINATOR="\r\n" MAX_LENGTH="24"/>
</RECORD>
<ROW>
<COLUMN SOURCE="1" NAME="DepartmentID" xsi:type="SQLSMALLINT"/>
<COLUMN SOURCE="2" NAME="Name" xsi:type="SQLNVARCHAR"/>
<COLUMN SOURCE="3" NAME="GroupName" xsi:type="SQLNVARCHAR"/>
<COLUMN SOURCE="4" NAME="ModifiedDate" xsi:type="SQLDATETIME"/>
</ROW>
</BCPFORMAT>
For information about the syntax of this format file, see Schema Syntax for XML Format Files. For information about character data, see Using Character Format to Import or Export Data.
B. Creating an XML format file for native data
The following example creates an XML format file, Department-n.xml, for the AdventureWorks 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 -x
|
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 is not specified, you must specify -U and -P to successfully log in.
|
At the Windows command prompt, enter the following bcp command:
bcp AdventureWorks.HumanResources.Department format nul -x -f Department-n..xml -n -T
The generated format file, Department-n.xml, contains the following XML elements:
<?xml version="1.0"?>
<BCPFORMAT xmlns="http://schemas.microsoft.com/sqlserver/2004/bulkload/format" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<RECORD>
<FIELD ID="1" xsi:type="NativeFixed" LENGTH="2"/>
<FIELD ID="2" xsi:type="NCharPrefix" PREFIX_LENGTH="2" MAX_LENGTH="100" COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
<FIELD ID="3" xsi:type="NCharPrefix" PREFIX_LENGTH="2" MAX_LENGTH="100" COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
<FIELD ID="4" xsi:type="NativeFixed" LENGTH="8"/>
</RECORD>
<ROW>
<COLUMN SOURCE="1" NAME="DepartmentID" xsi:type="SQLSMALLINT"/>
<COLUMN SOURCE="2" NAME="Name" xsi:type="SQLNVARCHAR"/>
<COLUMN SOURCE="3" NAME="GroupName" xsi:type="SQLNVARCHAR"/>
<COLUMN SOURCE="4" NAME="ModifiedDate" xsi:type="SQLDATETIME"/>
</ROW>
</BCPFORMAT>
For information about the syntax of this format file, see Schema Syntax for XML Format Files. For information about how to use native data, see Using Native Format to Import or Export Data.