05.11.2015 Views

Apress.Expert.Oracle.Database.Architecture.9i.and.10g.Programming.Techniques.and.Solutions.Sep.2005

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

498<br />

CHAPTER 12 ■ DATATYPES<br />

ops$tkyte@ORA10G> select * from t where varchar2_column = :varchar2_bv;<br />

CHAR_COLUMN<br />

VARCHAR2_COLUMN<br />

-------------------- --------------------<br />

Hello World<br />

Hello World<br />

Here, the search for the VARCHAR2 string worked, but the CHAR column did not. The<br />

VARCHAR2 bind variable will not be promoted to a CHAR(20) in the same way as a character<br />

string literal. At this point, many programmers form the opinion that “bind variables don’t<br />

work; we have to use literals.” That would be a very bad decision indeed. The solution is to<br />

bind using a CHAR type:<br />

ops$tkyte@ORA10G> variable char_bv char(20)<br />

ops$tkyte@ORA10G> exec :char_bv := 'Hello World';<br />

PL/SQL procedure successfully completed.<br />

ops$tkyte@ORA10G><br />

ops$tkyte@ORA10G> select * from t where char_column = :char_bv;<br />

CHAR_COLUMN<br />

VARCHAR2_COLUMN<br />

-------------------- --------------------<br />

Hello World<br />

Hello World<br />

ops$tkyte@ORA10G> select * from t where varchar2_column = :char_bv;<br />

no rows selected<br />

However, if you mix <strong>and</strong> match VARCHAR2 <strong>and</strong> CHAR, you’ll be running into this issue<br />

constantly. Not only that, but the developer is now having to consider the field width in her<br />

applications. If the developer opts for the RPAD() trick to convert the bind variable into something<br />

that will be comparable to the CHAR field (it is preferable, of course, to pad out the bind<br />

variable, rather than TRIM the database column, as applying the function TRIM to the column<br />

could easily make it impossible to use existing indexes on that column), she would have to be<br />

concerned with column length changes over time. If the size of the field changes, then the<br />

application is impacted, as it must change its field width.<br />

It is for these reasons—the fixed-width storage, which tends to make the tables <strong>and</strong><br />

related indexes much larger than normal, coupled with the bind variable issue—that I avoid<br />

the CHAR type in all circumstances. I cannot even make an argument for it in the case of the<br />

one-character field, because in that case it is really of no material difference. The VARCHAR2(1)<br />

<strong>and</strong> CHAR(1) are identical in all aspects. There is no compelling reason to use the CHAR type in<br />

that case, <strong>and</strong> to avoid any confusion, I “just say no,” even for the CHAR(1) field.<br />

Character String Syntax<br />

The syntax for the four basic string types is straightforward, as described in Table 12-1.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!