有两种方式指定数据的字符集,在控制文件通过CHARACTERSET选项,在OS中通过NLS_LANG环境变量
The default character set for all data files, if the CHARACTERSET parameter is not specified, is the session character set defined by the NLS_LANG parameter.
SQLLDR在导入时能根据数据库字符集自动转化为the database character set or the database national character set,要求数据库字符集必须包含数据文件字符集,否则不识别的字符将转为默认字符?
(一) CHARACTERSET Parameter
CHARACTERSET char_set_name
- 只有数据文件字符才受此选项影响
Only character data (fields in the SQL*Loader data types CHAR, VARCHAR, VARCHARC, numeric EXTERNAL, and the datetime and interval data types) is affected by the character set of the data file.
- 必须设置为Oracle支持的字符集
For UTF-16 Unicode encoding, use the name UTF16 rather than AL16UTF16. AL16UTF16, which is the supported Oracle character set name for UTF-16 encoded data, is only for UTF-16 data that is in big-endian byte order. However, because you are allowed to set up data using the byte order of the system where you create the data file, the data in the data file can be either big-endian or little-endian. Therefore, a different character set name (UTF16) is used. The character set name AL16UTF16 is also supported. But if you specify AL16UTF16 for a data file that has little-endian byte order, then SQL*Loader issues a warning message and processes the data file as little-endian.
- The CHARACTERSET parameter can be specified for primary data files and also for LOBFILEs and SDFs. All primary data files are assumed to be in the same character set. A CHARACTERSET parameter specified before the INFILE parameter applies to the entire list of primary data files. If the CHARACTERSET parameter is specified for primary data files, then the specified value will also be used as the default for LOBFILEs and SDFs. This default setting can be overridden by specifying the CHARACTERSET parameter with the LOBFILE or SDF specification.
- 此选项不影响INFILE * 的数据,它使用的是NLS_LANG设置值
The character set specified with the CHARACTERSET parameter does not apply to data specified with the INFILE clause in the control file. The control file is always processed using the character set specified for your session by the NLS_LANG parameter. Therefore, to load data in a character set other than the one specified for your session by the NLS_LANG parameter, you must place the data in a separate data file.
Any data included after the BEGINDATA statement is also assumed to be in the character set specified for your session by the NLS_LANG parameter.
- Control File Character Set
控制文件的字符集与NLS_LANG设置相同,如果控制文件字符集与数据文件字符集不同时要注意,控制文件中的Delimiters与comparison子句指定的字符可以被转为数据文件中的字符,你可以在控制文件中使用十六进制字符串而不是一般字符串
If the control file character set is different from the data file character set, then keep the following issue in mind. Delimiters and comparison clause values specified in the SQL*Loader control file as character strings are converted from the control file character set to the data file character set before any comparisons are made. To ensure that the specifications are correct, you may prefer to specify hexadecimal strings, rather than character string values.
如果使用UTF-16的十六进制字符串,会有big-endian与little-endian问题
If hexadecimal strings are used with a data file in the UTF-16 Unicode encoding, then the byte order is different on a big-endian versus a little-endian system. For example, "," (comma) in UTF-16 on a big-endian system is X'002c'. On a little-endian system it is X'2c00'. SQL*Loader requires that you always specify hexadecimal strings in big-endian format. If necessary, SQL*Loader swaps the bytes before making comparisons. This allows the same syntax to be used in the control file on both a big-endian and a little-endian system.
Record terminators for data files that are in stream format in the UTF-16 Unicode encoding default to "\n" in UTF-16 (that is, 0x000A on a big-endian system and 0x0A00 on a little-endian system). You can override these default settings by using the "STR 'char_str'" or the "STR x'hex_str'"specification on the INFILE line. For example, you could use either of the following to specify that 'ab' is to be used as the record terminator, instead of '\n'.
INFILE myfile.dat "STR 'ab'"
INFILE myfile.dat "STR x'00410042'"
- Character-Length Semantics
注意Oracle是使用的byte还是character为单位设置字符长度(nls_length_semantics=char|BYTE),如果数据库使用的是byte,可能在字符转换时出现长度不够的报错,这时你要么创建表时使用char长度,要么确保导入数据列的最大byte长度
The sizes of the database character types CHAR and VARCHAR2 can be specified in bytes (byte-length semantics) or in characters (character-length semantics). If they are specified in bytes, and data character set conversion is required, then the converted values may take more bytes than the source values if the target character set uses more bytes than the source character set for any character that is converted. This will result in the following error message being reported if the larger target value exceeds the size of the database column:
ORA-01401: inserted value too large for column
You can avoid this problem by specifying the database column size in characters and also by using character sizes in the control file to describe the data. Another way to avoid this problem is to ensure that the maximum column size is large enough, in bytes, to hold the converted value.
数据文件默认是使用byte的(如果指定了UTF16则默认为char), 可以在CHARACTERSET 选项后指定
Byte-length semantics are the default for all data files except those that use the UTF16 character set (which uses character-length semantics by default).To override the default you can specify CHAR or CHARACTER, as shown in the following syntax:
The LENGTH parameter applies to the syntax specification for primary data files and also to LOBFILEs and secondary data files (SDFs). A LENGTH specification before the INFILE parameters applies to the entire list of primary data files. The LENGTH specification specified for the primary data file is used as the default for LOBFILEs and SDFs. You can override that default by specifying LENGTH with the LOBFILE or SDF specification. Unlike the CHARACTERSET parameter, the LENGTH parameter can also apply to data contained within the control file itself (that is, INFILE * syntax).
You can specify CHARACTER instead of CHAR for the LENGTH parameter.
此选项会对以下类型生效:
If character-length semantics are being used for a SQL*Loader data file, then the following SQL*Loader data types will use character-length semantics: CHAR, VARCHAR, VARCHARC, DATE, EXTERNAL numerics (INTEGER, FLOAT, DECIMAL, and ZONED)
For the VARCHAR data type, the length subfield is still a binary SMALLINT length subfield, but its value indicates the length of the character string in characters.
不会对以下类型生效:
The following data types use byte-length semantics even if character-length semantics are being used for the data file, because the data is binary, or is in a special binary-encoded form in the case of ZONED and DECIMAL:INTEGER, SMALLINT, FLOAT, DOUBLE, BYTEINT,ZONED, DECIMAL, RAW, VARRAW, VARRAWC, GRAPHIC, GRAPHIC EXTERNAL, VARGRAPHIC
注意Position选项是按byte计算的,不论数据文件是否设置为char:
The start and end arguments to the POSITION parameter are interpreted in bytes, even if character-length semantics are in use in a data file. This is necessary to handle data files that have a mix of data of different data types, some of which use character-length semantics, and some of which use byte-length semantics. It is also needed to handle position with the VARCHAR data type, which has a SMALLINT length field and then the character data. The SMALLINT length field takes up a certain number of bytes depending on the system (usually 2 bytes), but its value indicates the length of the character string in characters.