Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Programming PL SQL.doc
Скачиваний:
3
Добавлен:
01.07.2025
Размер:
5 Мб
Скачать
☆

8.2.3 The nvarchar2 and nchar Datatypes

the NVARCHAR2 and NCHAR datatypes represent character data using the national character set rather than the database character set. In addition, they are always declared using character semantics (no CHAR qualifier applies). Other than those two differences, NVARCHAR2 and NCHAR are just like their counterparts, VARCHAR2 and CHAR. Use NVARCHAR2 for variable-length strings in the national character set, and use NCHAR for fixed-length strings in the national character set.

Following are some exampledeclarations:

line_of_text NVARCHAR2(2000); -- 2000 characters, variable-length

feature_name NCHAR(100); -- 100 characters, fixed-length

Prior to Oracle9i, the national character set could be any one of a large number of single- and multibyte character sets. Beginning with Oracle9i, the national character set used by NCHAR and NVARCHAR2 variables must be either UTF-8 (multibyte, variable-width) or UTF-16 (multibyte, fixed-width).

See Section 8.1.3 earlier in this chapter for a more detailed explanation of database and national character sets.

The NVARCHAR2 and NCHAR types have been improved in Oracle9i, and you may now freely mix them in expressions involving VARCHAR2 and CHAR. Prior to Oracle9i, for example, if you attempted to compare a VARCHAR2 value with an NVARCHAR2 value, PL/SQL would return a PLS-00561: character set mismatch error. In Oracle9i, such expressions are possible. Of course, you pay a penalty in terms of efficiency, because PL/SQL must implicitly convert the values to a common character set.

8.2.4 String Subtypes

PL/SQL supports several subtypes, listed in Table 8-2, that you can use when declaring character string variables. Many of these subtypes exist for the ostensible purpose of providing compatibility with the ANSI SQL standard. It's unlikely that you'll ever need to use these—we never do—but you should be aware that they exist.

Table 8-2. PL/SQL subtypes and their equivalents

Subtype

Equivalent PL/SQL type

CHAR VARYING

VARCHAR2

CHARACTER

CHAR

CHARACTER VARYING

VARCHAR2

NATIONAL CHAR

NCHAR

NATIONAL CHAR VARYING

NVARCHAR2

NATIONAL CHARACTER

NCHAR

NATIONAL CHARACTER VARYING

NVARCHAR2

NCHAR VARYING

NVARCHAR2

STRING

VARCHAR2

VARCHAR

VARCHAR2

Each subtype listed in the table is equivalent to the base PL/SQL type shown in the right-hand column. For example, the followingdeclarations all have the same effect:

feature_name VARCHAR2(100);

feature_name CHARACTER VARYING(100);

feature_name CHAR VARYING(100);

feature_name STRING(100);

The VARCHAR subtype deserves special mention. For years now Oracle has been threatening to change the meaning of VARCHAR (to something not equivalent to VARCHAR2) and warning against its use. We agree with Oracle's recommendation. If there's a possibility of VARCHAR's being changed, it's senseless to depend on its current behavior. Don't use VARCHAR; use VARCHAR2.

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]