Saturday 21 October 2017

Anchored Variable Declarations(%TYPE)

  • When we are declaring anchore data type variable in PL/SQL, that mean PL/SQL to set the datatype of that variable from the datatype of another element.
  • The '%TYPE' variables is use to anchore PL/SQL variables to the database type columns directly.

Syntax:
<variable name> <type attribute>%TYPE ;
type attribute is nothing but previously declare PL/SQL variable or Table.column name.

Declarations with %TYPE:

Anchored Variable Declarations(%TYPE):


DECLARE
/*V_EMPNAME is referring the table.column name*/
V_EMPNAME  EMP.ENAME%TYPE;
/*V_EMPNAME is referring the previously declare PL/SQL variable*/
V_FULL_NAME    V_EMPNAME%TYPE;          
BEGIN
NULL;
END;


Nesting Usages of the %TYPE Attribute:
See the below nest usages of %TYPE example


DECLARE
   /* The base variable */
   V_EMPNAME  EMP.ENAME%TYPE;

   /* Anchored to V_EMPNAME */
   V_FST_NAME    V_EMPNAME%TYPE;   

   /* Anchored to V_FST_NAME*/
   V_FULL_NAME    V_FST_NAME%TYPE;
  
BEGIN
null;
END;


Why we will use %TYPE?
If the data type of a particular column is changing in the back end process of the serever,
either with data type or width, will never effect the PL/SQL blocks or it will not effect any operations.





No comments:

Post a Comment