Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
37
Добавлен:
16.04.2013
Размер:
5.97 Mб
Скачать

Converting PL/SQL Datatypes

DECLARE

 

SUBTYPE b1 IS

BOOLEAN;

SUBTYPE b2 IS

BOOLEAN;

finished b1;

-- Different subtypes,

debugging b2;

-- both based on BOOLEAN.

BEGIN

debugging := finished; -- They can be assigned to each other. END;

Different subtypes are also interchangeable if their base types are in the same datatype family. For example, given the following declarations, the value of verb can be assigned to sentence:

DECLARE

SUBTYPE Word IS CHAR(15);

SUBTYPE Text IS VARCHAR2(1500);

verb

Word;

-- Different subtypes

sentence

Text(150);

--

of types from the same family

BEGIN

 

 

 

sentence

:= verb;

--

can be assigned, if not too long.

END;

 

 

 

Converting PL/SQL Datatypes

Sometimes it is necessary to convert a value from one datatype to another. For example, to use a DATE value in a report, you must convert it to a character string. PL/SQL supports both explicit and implicit (automatic) datatype conversion. To ensure your program does exactly what you expect, use explicit conversions wherever possible.

Explicit Conversion

To convert values from one datatype to another, you use built-in functions. For example, to convert a CHAR value to a DATE or NUMBER value, you use the function TO_DATE or TO_NUMBER, respectively. Conversely, to convert a DATE or NUMBER value to a CHAR value, you use the function TO_CHAR. For more information about these functions, see Oracle Database SQL Reference.

Using explicit conversions, particularly when passing parameters to subprograms, can avoid unexpected errors or wrong results. For example, the TO_CHAR function lets you specify the format for a DATE value, rather than relying on language settings in the database. Including an arithmetic expression among strings being concatenated with the || operator can produce an error unless you put parentheses or a call to TO_CHAR around the entire arithmetic expression.

Implicit Conversion

When it makes sense, PL/SQL can convert the datatype of a value implicitly. This lets you use literals, variables, and parameters of one type where another type is expected. For example, you can pass a numeric literal to a subprogram that expects a string value, and the subprogram receives the string representation of the number.

In the following example, the CHAR variables start_time and finish_time hold string values representing the number of seconds past midnight. The difference between those values must be assigned to the NUMBER variable elapsed_time. PL/SQL converts the CHAR values to NUMBER values automatically.

DECLARE

start_time CHAR(5);

3-18 PL/SQL User's Guide and Reference

Converting PL/SQL Datatypes

finish_time CHAR(5); elapsed_time NUMBER(5);

BEGIN

/* Get system time as seconds past midnight. */

SELECT TO_CHAR(SYSDATE,'SSSSS') INTO start_time FROM sys.dual; -- do something

/* Get system time again. */

SELECT TO_CHAR(SYSDATE,'SSSSS') INTO finish_time FROM sys.dual; /* Compute elapsed time in seconds. */

elapsed_time := finish_time - start_time; INSERT INTO results VALUES (elapsed_time, ...);

END;

Before assigning a selected column value to a variable, PL/SQL will, if necessary, convert the value from the datatype of the source column to the datatype of the variable. This happens, for example, when you select a DATE column value into a VARCHAR2 variable.

Likewise, before assigning the value of a variable to a database column, PL/SQL will, if necessary, convert the value from the datatype of the variable to the datatype of the target column. If PL/SQL cannot determine which implicit conversion is needed, you get a compilation error. In such cases, you must use a datatype conversion function.

Table 3–1 shows which implicit conversions PL/SQL can do.

Notes:

The labels PLS_INT and BIN_INT represent the types PLS_INTEGER and BINARY_INTEGER in the table. You cannot use them as abbreviations in code.

The table lists only types that have different representations. Types that have the same representation, such as CLOB and NCLOB, CHAR and NCHAR, and VARCHAR and NVARCHAR2, can be substituted for each other.

You can implicitly convert between CLOB and NCLOB, but be careful because this can be an expensive operation. To make clear that this conversion is intended, you can use the conversion functions TO_CLOB and TO_NCLOB.

TIMESTAMP, TIMESTAMP WITH TIME ZONE, TIMESTAMP WITH LOCAL TIME ZONE, INTERVAL DAY TO SECOND, and INTERVAL YEAR TO MONTH can all be converted using the same rules as the DATE type. However, because of their different internal representations, these types cannot always be converted to each other. See Oracle Database SQL Reference for details on implicit conversions between different date and time types.

Table 3–1 Implicit Conversions

 

BIN_INT BLOB CHAR CLOB DATE

LONG

NUMBER

PLS_INT RAW UROWID

VARCHAR2

 

 

 

 

 

 

 

 

 

 

BIN_INT

 

X

 

X

X

X

 

 

X

BLOB

 

 

 

 

 

 

X

 

 

CHAR

X

X

X

X

X

X

X

X

X

CLOB

 

X

 

 

 

 

 

 

X

DATE

 

X

 

X

 

 

 

 

X

LONG

 

X

 

 

 

 

X

 

X

NUMBER

X

X

 

X

 

X

 

 

X

PLS_INT

X

X

 

X

X

 

 

 

X

PL/SQL Datatypes 3-19

Converting PL/SQL Datatypes

Table 3–1 (Cont.) Implicit Conversions

 

BIN_INT BLOB

CHAR CLOB

DATE

LONG

NUMBER

PLS_INT RAW UROWID VARCHAR2

 

 

 

 

 

 

 

 

 

 

RAW

X

X

 

 

X

 

 

 

X

UROWID

 

X

 

 

 

 

 

 

X

VARCHA

X

X

X

X

X

X

X

X

X

R2

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

It is your responsibility to ensure that values are convertible. For instance, PL/SQL can convert the CHAR value '02-JUN-92' to a DATE value but cannot convert the CHAR value 'YESTERDAY' to a DATE value. Similarly, PL/SQL cannot convert a VARCHAR2 value containing alphabetic characters to a NUMBER value.

Choosing Between Implicit and Explicit Conversion

Relying on implicit datatype conversions is a poor programming practice because they can be slower and the conversion rules might change in later software releases. Implicit conversions are context-sensitive and not always predictable. For best reliability and maintainability, use datatype conversion functions.

DATE Values

When you select a DATE column value into a CHAR or VARCHAR2 variable, PL/SQL must convert the internal binary value to a character value. PL/SQL calls the function TO_CHAR, which returns a character string in the default date format. To get other information, such as the time or Julian date, call TO_CHAR with a format mask.

A conversion is also necessary when you insert a CHAR or VARCHAR2 value into a DATE column. PL/SQL calls the function TO_DATE, which expects the default date format. To insert dates in other formats, call TO_DATE with a format mask.

RAW and LONG RAW Values

When you select a RAW or LONG RAW column value into a CHAR or VARCHAR2 variable, PL/SQL must convert the internal binary value to a character value. In this case, PL/SQL returns each binary byte of RAW or LONG RAW data as a pair of characters. Each character represents the hexadecimal equivalent of a nibble (half a byte). For example, PL/SQL returns the binary byte 11111111 as the pair of characters 'FF'. The function RAWTOHEX does the same conversion.

A conversion is also necessary when you insert a CHAR or VARCHAR2 value into a RAW or LONG RAW column. Each pair of characters in the variable must represent the hexadecimal equivalent of a binary byte. Otherwise, PL/SQL raises an exception.

3-20 PL/SQL User's Guide and Reference

Тут вы можете оставить комментарий к выбранному абзацу или сообщить об ошибке.

Мы не исправляем ошибки в тексте (почему?), но будем благодарны, если вы все же напишите об ошибках.

Соседние файлы в папке Oracle 10g