Skip to main content

Benefits of %TYPE and %ROWTYPE in Oracle PL/SQL Anonymous Block #Diksha Oracle Training Centre

 Benefits of  %TYPE and %ROWTYPE in Oracle PL/SQL Anonymous Block.

 

Hello Everyone,

Today I will discuss about %TYPE and %ROWTYPE in Oracle. In my previous blog I already discussed about uses of cursors in Anonymous PL/SQL block. I suggest you to please go through below blog for your reference so that you can easily understand %TYPE and %ROWTYPE in PL/SQL anonymous block.

https://dikshaoracletraining.blogspot.com/2020/10/cursor-and-types-of-cursor-in-oracle.html

Let’s Start with  both attribute  %TYPE and %ROWTYPE then we will write some Anonymous PL/SQL block  by using %TYPE and % ROWTYPE and see its advantage.

 %TYPE : %TYPE  is mainly used to defined the data type of variable as the column name data type specified for a table. If table column has data type varchar2 (50) then %type variable automatically take the data type that is varchar2 (50) no need to define data type while writing PL/SQL block as we simply want to know the table name which can be used in both Anonymous block and named block and for column we don’t need to worry because we can take multiple columns of different tables by simply using %TYPE.  From %TYPE we get the benefits that we don’t need to see the data type of each table and for any column we simply use %TYPE so that it will automatically take the data type from corresponding table.

So for programmer it’s very easy to take automatically data type from any tables that we need to use in procedure and it will save the time as well as programs looks in structured way as we are not defining data types in variables.

Syntax for defining %TYPE for any variable:

v_name Employee.Name%TYPE;

Here variable v_name automatically takes the data type of name from employee table . In previous blog we mentioned that data type should be match from table column when we declare any variable in Anonymous or Named block. For this we need to check the data type of table then same data type we use in variable like v_name varchar2(50) which is lengthy process , so now as we know %type automatically takes the data type for any column in tables so we can use %TYPE for any table columns for variable declaration.

Note: We can use %TYPE in Anonymous block as well as in Named block such as stored procedures, functions packages etc

Example of Anonymous Block by using %TYPE :

DECLARE

v_Name  employee.name%TYPE;

v_address employee.address%TYPE;

v_salary employee.salary%TYPE;

BEGIN

SELECT Name,address,salary INTO v_Name,v_address,v_salary

FROM Employee

WHERE ROWNUM = 1;

DBMS_OUTPUT.PUT_LINE('Below is the name ,address and salary for employee from employee table');

DBMS_OUTPUT.PUT_LINE(v_name||' '||v_address||' '||v_salary);

END;

/


Here we can see the benefits of using %TYPE as we take two columns name and address from employee table but not mentioned data type in variable as %TYPE automatically takes the data type of both columns. We can also take different columns from other tables as well if we want to make complex query and required data types from multiple table.

In Above procedure we not used any loop so it will give only 1 record that’s why we use ROWNUM.

Note: We can use loop to display multiple records by using loop.

Below is the output of above program.


 

Another Program using  Explicit Cursor and %TYPE.

Example:

Declare

    CURSOR C1 IS SELECT name, address , salary FROM employee;

    v_name          employee.name%TYPE;

    v_address       employee.address%TYPE;

    v_salary        employee.salary%TYPE;

BEGIN

    OPEN C1;

    LOOP

        FETCH C1 INTO v_name,v_address,v_salary;

        EXIT WHEN C1%NOTFOUND;

        DBMS_OUTPUT.PUT_LINE( v_name ||' '|| v_address||' '||v_salary);

    END LOOP;

    CLOSE C1;

END;

Here we will get multiple outputs as we use loop in above program. Below is the output of above program.

 


 

%ROWTYPE: %ROWTYPE is dynamic as it will take the data types of all the columns in a table not like %TYPE where we need to mention it for several columns. In other word it is used in cursors to declare a single variable to contain a single record from the result set of a cursor or table without needing to specify individual variables. So benefits of %ROWTPE we can have data types of the entire column for any tables.

Syntax for defining %ROWTPE is as below :

v_employee employee%ROWTYPE;

Here we will have the data types of all the columns present in Employee Table in v_employee variable . No need to define data type of individual column on variable as %ROWTPE is dynamic and it will take all the columns data type from employee table.


Example of  PL/SQL Anonymous Block by using %ROWTYPE.

 

DECLARE

v_employee employee%ROWTYPE;

BEGIN

SELECT name,address,SALARY

 INTO v_employee.name,v_employee.address,v_employee.salary

FROM Employee

WHERE ROWNUM = 1;

DBMS_OUTPUT.PUT_LINE('Employee details by using %ROWTYPE');

DBMS_OUTPUT.PUT_LINE(v_employee.name||' '||v_employee.address ||' '||v_employee.salary);

END;

Here we see we define only 1 variable by using %ROWTYPE but it has all the columns data type from employee table , I used 3 columns but we can use multiple columns to fetch and display the record by using %ROWTYPE. As we haven’t used any loop so it will give only 1 record that’s why in select statement we use ROWNUM=1 .

Below is the output of above Program:


 

Let’s create Anonymous PL/SQL block by using explicit Cursor with %ROWTYPE to display multiple records.

DECLARE

    CURSOR C1 IS SELECT name, address,salary FROM employee;

    v_employee           C1%ROWTYPE;

BEGIN

    OPEN C1;

    LOOP

        FETCH C1 INTO v_employee;

        EXIT WHEN C1%NOTFOUND;

        DBMS_OUTPUT.PUT_LINE( v_employee.name ||' '||v_employee.address || ' '||v_employee.salary);

    END LOOP;

    CLOSE C1;

END;

/

Here we will get multiple records as we used for cursor loop, Here we see v_employee  is %ROWTYPE and through cursor we fetch the data in v_employee variable and output we can get name  address and salary by using v_employee variable which uses %ROWTYPE that is dynamic and can hold multiple columns from tables as well as cursor.

Below is the output of above Program.



Hope you have clear picture about %TYPE and %ROWTYPE uses and benefits as we seen in the PL/SQL Anonymous block  via using cursor or plain SQL. Also we can use %TYPE and %ROWTYPE in Named block such as functions, packages, procedures. Once I start topic Named block in PL/SQL I will use both attributes for procedures , functions etc. Please try to practice and write some complex PL/SQL Anonymous block by using above methods and let me know if you have any doubts.

Please go through this blog carefully as this topic is important and in interview some questions asked from this topic as well. Also use this attribute as it can be used in both PL/SQL Anonymous as well as Named block such as Procedure , Functions, Packages etc.

Thanks.

 

Comments

Post a Comment

Popular posts from this blog

SQL and Classification of SQL in Oracle Database #Diksha Oracle Training Centre

  SQL and Classification of SQL in Oracle Database.   SQL is Structured Query Language , which is used for storing, manipulating and retrieving data stored in a relational database .SQL is the standard language for RDBMS. All the Relational Database Management Systems (RDMS) like Oracle, MySQL, Sybase, Informix, IBM DB2 and Microsoft SQL Server use SQL as their standard database language. Oracle is one of the more secured database as compared to other databases. Importance of   SQL : SQL and PL/SQL is a backend process where all data is stored and retrieved in GUI which created either by any programming languages like Java, C++, PHP etc. so we need to have very secure database so that there will be no impact for users. SQL allows users to access data in the relational database management systems. SQL is used to communicate with a database.SQL and PL/SQL allows users to create and drop databases tables , views , stored procedures , functions , packages , trigger et...

Materialized View uses and Benefits in Database #DOTC_Mdb

Materialized View uses and Benefits in Database. Hello Everyone, Today we will discuss about Materialized view as it’s play important role in database. We already discussed about Simple Views and complex views in my previous blog. Before Materialized view go through my previous blog which related to simple view. https://dheeraj60.blogspot.com/2020/05/benefits-of-creating-and-using-view-in.html As we know View is not a database object and not like table which is stored in database, but view can be created from base table a view is an SQL statement that’s stored in the database. This statement, or view, has a name.A view looks and acts a lot like a table. It has columns and rows, and can be included in SELECT queries just like a table. In other word we can say View is a virtual/logical table which is basically used for security purpose. Let’s understand   about   Materialized view : A materialized view is a view that stores the results of the view’s query. Whenever you que...

Top 50 Interview Questions On SQL and PL/SQL #DOTC_Mdb

                    Top 50 Interview Questions On SQL and PL/SQL. Today we will Discuss Top 50 interview questions and answers of SQL and PL/SQL which is frequently asked in interview.     Question 1: What is SQL and Classification of SQL? Answer SQL is a Structure Query Language which is vastly used in RDBMS database like Oracle, Sybase, DB2 , Microsoft SQL server etc.   Classification of SQL is as below: DDL (Data Definition Language):  Commands are  create , alter , drop , truncate etc DML (Data Manipulation Language) : Commands are  insert,update and delete . TCL (Transaction Control Language ) : Commands are  Commit , Rollback and Save point. DCL (Data Control Language) : Commands are Grant , Revoke Question 2:    What is meant by Joins? What are the types of join? Answer Joins are basically used to extract/get data from multiple tables using s...