Home
VARCHAR2 should be used
Description
The rule "VARCHAR2 should be used" states that the VARCHAR2 data type should be used when creating variables, columns, and parameters in PL/SQL code. VARCHAR2 is a data type that stores variable-length character strings up to 4000 characters in length. It is the preferred data type for storing character strings in PL/SQL code, as it is more efficient than the CHAR data type and can be used to store strings of varying lengths. Using VARCHAR2 instead of CHAR can help reduce the amount of memory used and improve the performance of the code.
Key Benefits
- Storage Size: VARCHAR2 can store up to 4000 bytes of data, which is more than enough for most applications. :
- Flexibility: VARCHAR2 can be used to store both character and numerical data. :
- Performance: VARCHAR2 is faster than other data types when it comes to data retrieval. :
Non-compliant Code Example
DECLARE firstname VARCHAR(75); --Non compliant code (VARCHAR is used) lastname VARCHAR(75); --Non compliant code (VARCHAR is used) landmark NVARCHAR2(75); BEGIN SELECT FIRSTNAME, LASTNAME, LANDMARK INTO firstname,lastname,landmark FROM CUSTOMERS; END;
Compliant Code Example
DECLARE firstname VARCHAR2(75); --Compliant code (VARCHAR2 is used) lastname VARCHAR2(75); --Compliant code (VARCHAR2 is used) landmark NVARCHAR2(75); BEGIN SELECT FIRSTNAME, LASTNAME, LANDMARK INTO firstname,lastname,landmark FROM CUSTOMERS; END;