使用 Unicode 本机格式导入或导出数据 (SQL Server)Use Unicode Native Format to Import or Export Data (SQL Server)

适用对象:是SQL Server 是Azure SQL 数据库 是Azure Synapse Analytics (SQL DW) 是并行数据仓库APPLIES TO: yesSQL Server yesAzure SQL Database yesAzure Synapse Analytics (SQL DW) yesParallel Data Warehouse

当必须将信息从一个 MicrosoftMicrosoft SQL ServerSQL Server 复制到另一个时,Unicode 本机格式将非常有用。Unicode native format is helpful when information must be copied from one MicrosoftMicrosoft SQL ServerSQL Server installation to another. 为非字符数据使用本机格式可以节省时间,并消除与字符格式之间不必要的数据类型转换。The use of native format for noncharacter data saves time, eliminating unnecessary conversion of data types to and from character format. 在使用不同代码页的服务器之间大容量传输数据时,为所有字符数据使用 Unicode 字符格式可以防止丢失任何扩展字符。The use of Unicode character format for all character data prevents loss of any extended characters during bulk transfer of data between servers using different code pages. 可以通过任何批量导入方法读取 Unicode 本机格式的数据文件。A data file in Unicode native format can be read by any bulk-import method.

通过使用包含扩展字符或 DBCS 字符的数据文件在 SQL ServerSQL Server 的多个实例之间大容量传输数据时,建议使用 Unicode 本机格式。Unicode native format is recommended for the bulk transfer of data between multiple instances of SQL ServerSQL Server by using a data file that contains extended or DBCS characters. 对于非字符数据,Unicode 本机格式使用本机(数据库)数据类型。For noncharacter data, Unicode native format uses native (database) data types. 对于字符数据(如 charncharvarcharnvarchartextvarchar(max)nvarchar(max)ntext),Unicode 本机格式使用 Unicode 字符数据格式。For character data, such as char, nchar, varchar, nvarchar, text, varchar(max), nvarchar(max), and ntext, the Unicode native format uses Unicode character data format.

在 Unicode 本机格式数据文件中存储为 SQLVARIANT 的 sql_variant 数据的运算方式与在本机格式数据文件中的运算方式相同,只是 charvarchar 值需转换为 ncharnvarchar,这使得受影响列所需的存储量加倍。The sql_variant data that is stored as a SQLVARIANT in a Unicode native-format data file operates in the same manner as it does in a native-format data file, except that char and varchar values are converted to nchar and nvarchar, which doubles the amount of storage required for the affected columns. 这些值的原始元数据被保留,当批量导入到表列时,这些值将转换回其原始的 charvarchar 数据类型。The original metadata is preserved, and the values are converted back to their original char and varchar data type when bulk imported into a table column.

本主题内容:In this Topic:
Unicode 本机格式的命令选项Command Options for Unicode Native Format
示例测试条件Example Test Conditions
 ● 示例表 ● Sample Table
 ● 示例非 XML 格式化文件 ● Sample Non-XML Format File
示例Examples
 ● 使用 bcp 和 Unicode 本机格式导出数据 ● Using bcp and Unicode Native Format to Export Data
 ● 在不使用格式化文件的情况下使用 bcp 和 Unicode 本机格式导入数据 ● Using bcp and Unicode Native Format to Import Data without a Format File
 ● 在使用非 XML 格式化文件的情况下使用 bcp 和 Unicode 本机格式导入数据 ● Using bcp and Unicode Native Format to Import Data with a Non-XML Format File
 ● 在不使用格式化文件的情况下使用 BULK INSERT 和 Unicode 本机格式 ● Using BULK INSERT and Unicode Native Format without a Format File
 ● 在使用非 XML 格式化文件的情况下使用 BULK INSERT 和 Unicode 本机格式 ● Using BULK INSERT and Unicode Native Format with a Non-XML Format File
 ● 在使用非 XML 格式化文件的情况下使用 OPENROWSET 和 Unicode 本机格式 ● Using OPENROWSET and Unicode Native Format with a Non-XML Format File
相关任务Related Tasks

Unicode 本机格式的命令选项Command Options for Unicode Native Format

可以使用 bcpBULK INSERTINSERT ... 将 Unicode 本机格式数据导入表中SELECT * FROM OPENROWSET(BULK...)。对于 bcp 命令或 BULK INSERT 语句,你可以在语句中指定数据格式。You can import Unicode native format data into a table using bcp, BULK INSERT or INSERT ... SELECT * FROM OPENROWSET(BULK...). For a bcp command or BULK INSERT statement, you can specify the data format in the statement. 对于 INSERT ...SELECT * FROM OPENROWSET(BULK...) 语句,必须在格式化文件中指定数据格式。For an INSERT ... SELECT * FROM OPENROWSET(BULK...) statement, you must specify the data format in a format file.

下列命令选项支持 Unicode 本机格式:Unicode native format is supported by the following command options:

CommandCommand 选项Option 描述Description
bcpbcp -N-N 使 bcp 实用工具使用 Unicode 本机格式,将为所有非字符数据使用本机(数据库)数据类型,为所有字符(charncharvarcharnvarchartextntext)数据使用 Unicode 字符数据格式。Causes the bcp utility to use the Unicode native format, which uses native (database) data types for all noncharacter data and Unicode character data format for all character (char, nchar, varchar, nvarchar, text, and ntext) data.
BULK INSERTBULK INSERT DATAFILETYPE ='widenative'DATAFILETYPE ='widenative' 批量导入数据时使用 Unicode 本机格式。Uses Unicode native format when bulk importing data.
OPENROWSETOPENROWSET N/AN/A 必须使用格式化文件Must use a format file

备注

或者,您可以在格式化文件中为每个字段指定格式设置。Alternatively, you can specify formatting on a per-field basis in a format file. 有关详细信息,请参阅 用来导入或导出数据的格式化文件 (SQL Server)For more information, see Format Files for Importing or Exporting Data (SQL Server).

示例测试条件Example Test Conditions

本主题中的示例基于下面定义的表和格式化文件。The examples in this topic are based on the table, and format file defined below.

示例表Sample Table

以下脚本将创建测试数据库、名为 myWidenative 的表并用一些初始值填充表。The script below creates a test database, a table named myWidenative and populates the table with some initial values. 在 Microsoft SQL Server Management StudioSQL Server Management Studio (SSMS) 中执行以下 Transact-SQL:Execute the following Transact-SQL in Microsoft SQL Server Management StudioSQL Server Management Studio (SSMS):

CREATE DATABASE TestDatabase;
GO

USE TestDatabase;
CREATE TABLE dbo.myWidenative ( 
    PersonID smallint NOT NULL,
    FirstName nvarchar(25) NOT NULL,
    LastName nvarchar(30) NOT NULL,
    BirthDate date,
    AnnualSalary money
);

-- Populate table
INSERT TestDatabase.dbo.myWidenative
VALUES 
(1, N'ϴAnthony', N'Grosse', '02-23-1980', 65000.00),
(2, N'❤Alica', N'Fatnowna', '11-14-1963', 45000.00),
(3, N'☎Stella', N'Rossenhain', '03-02-1992', 120000.00);

-- Review Data
SELECT * FROM TestDatabase.dbo.myWidenative;

示例非 XML 格式化文件Sample Non-XML Format File

SQL Server 支持两种类型的格式化文件:非 XML 格式和 XML 格式。SQL Server support two types of format file: non-XML format and XML format. 非 XML 格式是 SQL Server 早期版本支持的原始格式。The non-XML format is the original format that is supported by earlier versions of SQL Server. 有关详细信息,请查看 非 XML 格式化文件 (SQL Server)Please review Non-XML Format Files (SQL Server) for detailed information. 下面的命令基于 的架构使用 bcp 实用工具 myWidenative.fmt生成非 XML 格式化文件 myWidenativeThe following command will use the bcp utility to generate a non-xml format file, myWidenative.fmt, based on the schema of myWidenative. 若要使用 bcp 命令创建格式化文件,请指定 format 参数,并使用 nul 而不是数据文件路径。To use a bcp command to create a format file, specify the format argument and use nul instead of a data-file path. 格式化选项还需要 -f 选项。The format option also requires the -f option. 此外,对于本示例,限定符 c 用于指定字符数据, T 用于指定使用集成安全性的受信任连接。In addition, for this example, the qualifier c is used to specify character data, and T is used to specify a trusted connection using integrated security. 在命令提示符处输入以下命令:At a command prompt, enter the following commands:

bcp TestDatabase.dbo.myWidenative format nul -f D:\BCP\myWidenative.fmt -T -N

REM Review file
Notepad D:\BCP\myWidenative.fmt

重要

确保非 XML 格式化文件以回车符/换行符结尾。Ensure your non-XML format file ends with a carriage return\line feed. 否则可能会收到以下错误消息:Otherwise you will likely receive the following error message:

SQLState = S1000, NativeError = 0
Error = [Microsoft][ODBC Driver 13 for SQL Server]I/O error while reading BCP format file

示例Examples

下面的示例使用上面创建的数据库和格式化文件。The examples below use the database, and format files created above.

使用 bcp 和 Unicode 本机格式导出数据Using bcp and Unicode Native Format to Export Data

-N 切换和 OUT 命令。-N switch and OUT command. 请注意:此示例中创建的数据文件将用于所有后续示例中。Note: the data file created in this example will be used in all subsequent examples. 在命令提示符处输入以下命令:At a command prompt, enter the following commands:

bcp TestDatabase.dbo.myWidenative OUT D:\BCP\myWidenative.bcp -T -N

REM Review results
NOTEPAD D:\BCP\myWidenative.bcp

在不使用格式化文件的情况下使用 bcp 和 Unicode 本机格式导入数据Using bcp and Unicode Native Format to Import Data without a Format File

-N 切换和 IN 命令。-N switch and IN command. 在命令提示符处输入以下命令:At a command prompt, enter the following commands:

REM Truncate table (for testing)
SQLCMD -Q "TRUNCATE TABLE TestDatabase.dbo.myWidenative;"

REM Import data
bcp TestDatabase.dbo.myWidenative IN D:\BCP\myWidenative.bcp -T -N

REM Review results is SSMS

在使用非 XML 格式化文件的情况下使用 bcp 和 Unicode 本机格式导入数据Using bcp and Unicode Native Format to Import Data with a Non-XML Format File

-N-f 切换以及 IN 命令。-N and -f switches and IN command. 在命令提示符处输入以下命令:At a command prompt, enter the following commands:

REM Truncate table (for testing)
SQLCMD -Q "TRUNCATE TABLE TestDatabase.dbo.myWidenative;"

REM Import data
bcp TestDatabase.dbo.myWidenative IN D:\BCP\myWidenative.bcp -f D:\BCP\myWidenative.fmt -T

REM Review results is SSMS

在不使用格式化文件的情况下使用 BULK INSERT 和 Unicode 本机格式Using BULK INSERT and Unicode Native Format without a Format File

DATAFILETYPE 参数。DATAFILETYPE argument. 在 Microsoft SQL Server Management StudioSQL Server Management Studio (SSMS) 中执行以下 Transact-SQL:Execute the following Transact-SQL in Microsoft SQL Server Management StudioSQL Server Management Studio (SSMS):

TRUNCATE TABLE TestDatabase.dbo.myWidenative; -- for testing
BULK INSERT TestDatabase.dbo.myWidenative
    FROM 'D:\BCP\myWidenative.bcp'
    WITH (
        DATAFILETYPE = 'widenative'
        );

-- review results
SELECT * FROM TestDatabase.dbo.myWidenative;

在使用非 XML 格式化文件的情况下使用 BULK INSERT 和 Unicode 本机格式Using BULK INSERT and Unicode Native Format with a Non-XML Format File

FORMATFILE 参数。FORMATFILE argument. 在 Microsoft SQL Server Management StudioSQL Server Management Studio (SSMS) 中执行以下 Transact-SQL:Execute the following Transact-SQL in Microsoft SQL Server Management StudioSQL Server Management Studio (SSMS):

TRUNCATE TABLE TestDatabase.dbo.myWidenative; -- for testing
BULK INSERT TestDatabase.dbo.myWidenative
   FROM 'D:\BCP\myWidenative.bcp'
   WITH (
        FORMATFILE = 'D:\BCP\myWidenative.fmt'
        );

-- review results
SELECT * FROM TestDatabase.dbo.myWidenative;

在使用非 XML 格式化文件的情况下使用 OPENROWSET 和 Unicode 本机格式Using OPENROWSET and Unicode Native Format with a Non-XML Format File

FORMATFILE 参数。FORMATFILE argument. 在 Microsoft SQL Server Management StudioSQL Server Management Studio (SSMS) 中执行以下 Transact-SQL:Execute the following Transact-SQL in Microsoft SQL Server Management StudioSQL Server Management Studio (SSMS):

TRUNCATE TABLE TestDatabase.dbo.myWidenative;  -- for testing
INSERT INTO TestDatabase.dbo.myWidenative
    SELECT *
    FROM OPENROWSET (
        BULK 'D:\BCP\myWidenative.bcp', 
        FORMATFILE = 'D:\BCP\myWidenative.fmt'  
        ) AS t1;

-- review results
SELECT * FROM TestDatabase.dbo.myWidenative;

相关任务Related Tasks

使用数据格式进行批量导入或批量导出To use data formats for bulk import or bulk export

另请参阅See Also

bcp Utility bcp Utility
BULK INSERT (Transact-SQL) BULK INSERT (Transact-SQL)
OPENROWSET (Transact-SQL) OPENROWSET (Transact-SQL)
数据类型 (Transact-SQL)Data Types (Transact-SQL)