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

A

Sample PL/SQL Programs

This appendix tells you where to find collections of sample PL/SQL programs, for your own study and testing.

Where to Find PL/SQL Sample Programs

You can find some sample programs in the PL/SQL demo directory. For the location of the directory, see the Oracle installation guide for your system. These samples are typically older ones based on the SCOTT schema, with its EMP and DEPT tables.

Most examples in this book have been made into complete programs that you can run under the HR sample schema, with its EMPLOYEES and DEPARTMENTS tables.

The Oracle Technology Network web site has a PL/SQL section with many sample programs to download, at http://otn.oracle.com/tech/pl_sql/. These programs demonstrate many language features, particularly the most recent ones. You can use some of the programs to compare performance of PL/SQL across database releases.

For examples of calling PL/SQL from other languages, see Oracle Database Java Developer's Guide and Pro*C/C++ Programmer's Guide.

Exercises for the Reader

Here are some PL/SQL programming constructs that are helpful to know. After learning from the sample programs in this book and on the web, check to see that you are familiar with writing each of these constructs.

An anonymous PL/SQL block.

A PL/SQL stored procedure.

A SQL CALL statement that invokes a stored procedure. An anonymous block that invokes the stored procedure.

A PL/SQL stored function.

A SQL query that calls the stored function.

A PL/SQL package.

An anonymous block or a stored procedure that calls a packaged procedure. A SQL query that calls a packaged function.

A SQL*Plus script, or a set of scripts called from a master script, that creates a set of procedures, functions, and packages.

Sample PL/SQL Programs A-1

Exercises for the Reader

A FORALL statement (instead of a regular loop) to issue multiple INSERT, UPDATE, or DELETE statements.

A-2 PL/SQL User's Guide and Reference

B

Understanding CHAR and VARCHAR2

Semantics in PL/SQL

This appendix explains the semantic differences between the CHAR and VARCHAR2 base types. These subtle but important differences come into play when you assign, compare, insert, update, select, or fetch character values.

This appendix contains these topics:

Assigning Character Values on page B-1

Comparing Character Values on page B-2

Inserting Character Values on page B-2

Selecting Character Values on page B-3

Assigning Character Values

When you assign a character value to a CHAR variable, if the value is shorter than the declared length of the variable, PL/SQL blank-pads the value to the declared length. Information about trailing blanks in the original value is lost. In the following example, the value assigned to last_name includes six trailing blanks, not just one:

last_name CHAR(10) := 'CHEN '; -- note trailing blank

If the character value is longer than the declared length of the CHAR variable, PL/SQL aborts the assignment and raises the predefined exception VALUE_ERROR. PL/SQL neither truncates the value nor tries to trim trailing blanks. For example, given the declaration

acronym CHAR(4);

the following assignment raises VALUE_ERROR:

acronym := 'SPCA

'; -- note trailing blank

When you assign a character value to a VARCHAR2 variable, if the value is shorter than the declared length of the variable, PL/SQL neither blank-pads the value nor strips trailing blanks. Character values are assigned intact, so no information is lost. If the character value is longer than the declared length of the VARCHAR2 variable, PL/SQL aborts the assignment and raises VALUE_ERROR. PL/SQL neither truncates the value nor tries to trim trailing blanks.

Understanding CHAR and VARCHAR2 Semantics in PL/SQL B-1

Comparing Character Values

Comparing Character Values

You can use the relational operators to compare character values for equality or inequality. Comparisons are based on the collating sequence used for the database character set. One character value is greater than another if it follows it in the collating sequence. For example, given the declarations

last_name1 VARCHAR2(10) := 'COLES'; last_name2 VARCHAR2(10) := 'COLEMAN';

the following IF condition is true:

IF last_name1 > last_name2 THEN ...

The SQL standard requires that two character values being compared have equal lengths. If both values in a comparison have datatype CHAR, blank-padding semantics are used: before comparing character values of unequal length, PL/SQL blank-pads the shorter value to the length of the longer value. For example, given the declarations

last_name1 CHAR(5) := 'BELLO';

last_name2 CHAR(10) := 'BELLO '; -- note trailing blanks

the following IF condition is true:

IF last_name1 = last_name2 THEN ...

If either value in a comparison has datatype VARCHAR2, non-blank-padding semantics are used: when comparing character values of unequal length, PL/SQL makes no adjustments and uses the exact lengths. For example, given the declarations

last_name1 VARCHAR2(10) := 'DOW';

last_name2 VARCHAR2(10) := 'DOW

'; -- note trailing blanks

the following IF condition is false:

IF last_name1 = last_name2 THEN ...

If a VARCHAR2 value is compared to a CHAR value, non-blank-padding semantics are used. But, remember, when you assign a character value to a CHAR variable, if the value is shorter than the declared length of the variable, PL/SQL blank-pads the value to the declared length. Given the declarations

last_name1 VARCHAR2(10) := 'STAUB';

last_name2 CHAR(10)

:= 'STAUB'; -- PL/SQL blank-pads value

the following IF condition is false because the value of last_name2 includes five trailing blanks:

IF last_name1 = last_name2 THEN ...

All string literals have datatype CHAR. If both values in a comparison are literals, blank-padding semantics are used. If one value is a literal, blank-padding semantics are used only if the other value has datatype CHAR.

Inserting Character Values

When you insert the value of a PL/SQL character variable into an Oracle database column, whether the value is blank-padded or not depends on the column type, not on the variable type.

B-2 PL/SQL User's Guide and Reference

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

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

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