Name

DAT-04: Do not hard-code VARCHAR2 lengths.

Synopsis

Sure, in general, you shouldn’t hard-code your datatypes; instead, you should rely on anchoring (see [DAT-02: Anchor variables to database datatypes using %TYPE and %ROWTYPE.]) and SUBTYPEs (see [DAT-03: Use SUBTYPE to standardize application-specific datatypes.]). This best practice is a special-case emphasis of those other best practices.

Don’t hard-code VARCHAR2 lengths, like:

DECLARE
   -- Gee, should be big enough
   big_string VARCHAR2(2000); 

Such a declaration may seem like a big-enough string, but it’s also a ticking time bomb in your application. Either %TYPE back to a database column, or define SUBTYPEs in a package specification that give names to standard VARCHAR2 usages.

Example

The basic problem with hard-coding a VARCHAR2 length is that stuff changes. Consider the maximum length possible for a VARCHAR2 column in the database. It was 2000 up through Oracle8, and then it expanded to 4000 in Oracle8i. The best way to handle this situation is to create a special type:

CREATE OR REPLACE app_types
IS
   SUBTYPE dbmax_vc2 IS VARCHAR2(2000);

Then, when you upgrade to Oracle8i, you simply change the definition of the SUBTYPE. All usages of that type stay the same.

Benefits

Your code is less likely to raise VALUE_ERROR exceptions over time.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset