Previous | Table of Contents | Next

Page 50

ANALYSIS
Had the variable age been declared as a BINARY_INTEGER, it would have been assigned a negative value and the result of the "Year 2000" error might show up in a manner far removed from the problem code. Because of the use of the subtype POSITIVE, you know instantly when an error occurs.

PLS_INTEGER

The PLS_INTEGER datatype is new in release 2.3 of PL/SQL and is used for declaring signed integer variables. Like the BINARY_INTEGER datatype, it also stores values in the range
_2,147,483,647 through 2,147,483,647. How is it different from a BINARY_INTEGER then? The PLS_INTEGER datatype uses the native machine instructions for performing computations. Thus PLS_INTEGER calculations are much faster than BINARY_INTEGER calculations, which use library functions to perform arithmetic.

The Syntax for the PLS_INTEGER Datatype

variable_name PLS_INTEGER;

In this syntax, variable_name is whatever name you want to give to the variable.

Here is a sample declaration:

my_integer PLS_INTEGER;
NOTE
Oracle recommends use of the PLS_INTEGER datatype over the BINARY_INTEGER datatype in all new applications.

DATE

The DATE datatype is used to store date and time values. A better name might perhaps be DATETIME because the time component is always there whether you use it or not. The range for date variables is from 1 Jan 4712 BC through 31 Dec 4712 AD. If you do not specify a time when assigning a value to a variable of type DATE, it will default to midnight (12:00:00 a.m.).

The Syntax for the DATE Datatype

variable_name DATE;

In this syntax, variable_name is the name that you want to give the variable.

Page 51

Here are some examples:

hire_date DATE;
emp_birthdate DATE;
TIP
Be careful when comparing dates. The time value can trip you up. Values in a database that are intended to contain only dates sometimes mistakenly have a time value stored with them, and this can cause comparisons for equality to fail. To be safe, if you really don't care about the time of day, you can use the TRUNC() function. For example, instead of
IF hire_date = fire_date THEN...
IF TRUNC(hire_date) = TRUNC(fire_date) THEN...
Use of the TRUNC() function will truncate any time value so that you are truly comparing only dates. This function will be discussed in more detail on Day 7.

BOOLEAN

The BOOLEAN datatype is used to store true/false values. Its range is only the two values, true and false.

The Syntax for the BOOLEAN Datatype

variable_name BOOLEAN;

In this syntax, variable_name is the name that you want to give this variable.

Here are some examples:

hired_fired_same_day BOOLEAN;
birthday_is_today BOOLEAN;
print_this_record BOOLEAN;

Boolean variables are often used as flag variables, and are also used to store the results of logical calculations. For example, if you needed to know if an employee's birthday was today, you could write this code:

birthday_is_today := (emp_birthdate = trunc(sysdate))

Then you could reference birthday_is_today anywhere in your code where you need to know this information. You would not have to recompare each time.

Page 52

TIP
Using boolean variables to store the results of comparisons can be a powerful construct. If you code a comparison only once, you can go back and change the calculation later without having to find and change several occurrences in your program. It can also add to readability. With a variable named birthday_is_today, you know why the comparison was made.

LONG

The LONG datatype in PL/SQL is just like VARCHAR2 except that it can store a maximum of 32760 bytes instead of 32767, which is actually 7 bytes less than the VARCHAR2 type. For this reason you should usually use VARCHAR2 instead.

The Syntax for the LONG Datatype

variable_name LONG(size);

In this syntax, variable_name is the name that you want to give this variable, and size is the size, in bytes, of the variable. This must be a number between 1 and 32760.

Here are some sample declarations:

emp_comment LONG(32760);
work_history LONG(10000);
NOTE
The PL/SQL LONG differs from the database version of a LONG in that a LONG database column can store 2 gigabytes of data, whereas the PL/SQL version can store only 32760 bytes.

RAW

The RAW datatype is used to store strings of byte-oriented data. The difference between a RAW and a VARCHAR2 string is that Oracle does no character set translation on raw data. Thus if you are retrieving raw data from an Oracle server using ASCII to a machine using the EBCDIC character set, no translation would be done.

The Syntax for the RAW Datatype

variable_name RAW(size);

In this syntax, variable_name is the name you want to give the variable, and size is the size, in bytes, of the variable. This must be a number between 1 and 32767.

Page 53

Here are some sample declarations:

sound_bytes RAW(32767);
some_data RAW(255);

Like VARCHAR2, the maximum length of a RAW variable is 32767 bytes.

NOTE
The database version of RAW allows only 255 bytes.

LONG RAW

The LONG RAW datatype is just like RAW except that the maximum length is 32760 bytes. That's not a misprint. In PL/SQL the maximum length of a LONG RAW really is 7 bytes less than the maximum length of a RAW.

The Syntax for the LONG RAW Datatype

variable_name LONG RAW(size);

In this syntax, variable_name is the name you want to give this variable, and size is the size, in bytes, of the variable. This must be a number between 1 and 32760.

Here are some examples:

sound_byte LONG RAW(20000);
a_picture LONG RAW(30000);

As with a RAW, no character set conversion is performed.

NOTE
The database version of LONG RAW allows up to 2 gigabytes of data.

ROWID

ROWID is a special datatype that enables you to store Oracle's internal key for database records.

The Syntax for the ROWID Datatype

variable_name ROWID;

In this syntax, variable_name is the name that you want to give the variable.

Here is an example:

employee_row_id ROWID;

Previous | Table of Contents | Next

Используются технологии uCoz