Anchor data types (Using %TYPE) in Oracle


I have seen some projects where the columns were declared as
  v_prod_description     VARCHAR2(100)
  which stores the product description up to 50 characters. And the value fetched into v_prod_description as
 
                   

         SELECT prod_description
                 INTO v_prod_description
             FROM product_mst
          WHERE prod_code = v_prod_code;
      
 
This works fine as long as the width of the underlying column prod_description in table product_mst is 100 chars. Suppose, the business wants to change the product description to 256 characters, then the column width can be altered to 256 characters.
  What happens if the same program is executed and the value of the product description is more than 100 characters?
 
The program will raise an exception and depending on the handler the program execution may stop or continue. But the important thing is that, the product description will not be available in the variable v_prod_description. In order to get the description, the variable width needs to be modified. If the variable is used in many places and in many programs then, as a developer all the programs need to be modified which is very difficult and time consuming.
  On the other hand, if the variable is declared as
 
                   
         v_prod_description    product_mst.prod_description%TYPE;
      
  then, the variable will change its data type automatically whenever the data type of prod_description column in the product_mst is changed.
  By following this practice, changing the data types of variables in the programs can be avoided when the underlying column’s data type is changed.

In the next post, will explain how can we use %ROWTYPE.

Comments