PL/SQL Best Practices [9][Data Structures]-Anchored Declarations
Anchor Declarations with %TYPE and %ROWTYPE
You must declare all variables and constants before you can use them. The best way to declare them is to use %TYPE and %ROWTYPE attributes to anchor your variable to an existing variable or database element. This way you avoid yet another kind of “hard-coding” in your programs.
Hard-Coded Declarations:
ename VARCHAR2(60);
total_sales NUMBER (10,2);Anchored Declarations:
ename emp.ename%TYPE;
total_sales sales_amt%TYPE;Benefits of Anchoring
Synchronize PL/SQL variables with database columns and rows.
* If a variable or parameter does represent database information in your program, always use %TYPE or %ROWTYPE.
* Keeps your programs in synch with database structures without having to make code changes.
Normalize/consolidate declarations of derived variables throughout your programs.
* Make sure that all declarations of dollar amounts or entity names are consistent.
* Change one declaration and upgrade all others with recompilation.

浙公网安备 33010602011771号