How to use%type and%rowtype in Oracle PL/SQL

Source: Internet
Author: User

1. using %TYPE

In many cases, aPL/SQL variable can be used to store data in a database table. In this case, the variable should have the same type as the table column. For example, thefirst_name column of the students table is of type VARCHAR2 (20), and we can declare a variable as follows:

DECLARE

V_firstname VARCHAR2 (20);

But What happens if the definition of the first_name column changes (for example, if the table changes, thecurrent type of first_name changes to VARCHAR2 (25))? That will cause all PL/SQL code that uses this column to be modified. If you have a lot of PL/SQL code, this kind of processing can be time-consuming and error-prone.

At this point, you can use the "%TYPE" property instead of hard-coding the variable type.

For example:

DECLARE

V_firstname Students.first_name%type;

Using the %type,v_firstname variable will be the same type as the first_name column of the students table (which can be understood as setting up the two states).

This type is determined every time an anonymous block or a named block runs the statement block and compiles the stored object (procedure, function, package, object class, and trigger).

Using %type is a very good programming style because it makes PL/SQL more flexible and more adaptable to database-defined updates.

2. using %rowtype

2.1 PL/SQL Records

The PL/SQL record type is similar to a struct in C, and is a composite type that is user-defined.

Records provide a mechanism for dealing with independent variables that are related to a monolithic unit. Please see:

DECLARE

V_studentid number (5);

V_firstname VARCHAR2 (20);

V_lastname VARCHAR2 (20);

These 3 variables are logically interrelated because they point to different fields in the students table. If you declare a record type for these variables, the relationship between them is obvious and can be handled as a unit.

DECLARE

/*define a record type to hold common student informationi*/

TYPE T_studentrecord is RECORD (

StudentID Number (5),

FirstName VARCHAR2 (20),

LastName VARCHAR2 (20);

/*declare A variable of this type.*/

V_studentinfo T_studentrecord;

2.2 Record Assignment

You can assign a value to a record with a SELECT statement, which retrieves the data from the database and stores that data in the record. Note that the fields in the record should match the fields in the query results list.

SELECT Studentid,firstname,lastname

Into V_studentinfo

from students where studentid=32;

2.3 Using %rowtype

It is common practice to declare a record in PL/SQL as a database row of the same type. PL/SQL provides the %rowtype operator, which makes the operation more convenient.

For example:

DECLARE

V_roomrecord Rooms%rowtype;

A record is defined, and the fields in the record correspond to the columns in the rooms table.

How to use%type and%rowtype in Oracle PL/SQL

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.