%type,%rowtype, record difference

Source: Internet
Author: User

In many cases, a PL/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, the first_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, the current 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 will be defined, and the fields in the record will correspond to the columns in the rooms table

Cases:

Declare
V_rowtype V%rowtype;
Begin
Select * into V_rowtype from v Where id=2;---Declare
V_rowtype V%rowtype;
Begin
Select * into V_rowtype from v where id=2/;----If you remove the where id=2 error (here because%rowtype can only store one row of records!)
Dbms_output.put_line (': ' | | V_rowtype.id);
End;

Dbms_output.put_line (': ' | | V_rowtype.id);
End;
%rowtype
Use the%rowtype property to declare a table-or cursor-based row object, also known as a record object, such as the following example:

Declare
V_rowtype V%rowtype;
Cursor V_test_cursor is
SELECT * from v where id=1;
Begin
Open v_test_cursor;
Loop
Fetch V_test_cursor
into V_rowtype;
Dbms_output.put_line (' This is: ' | | v_rowtype.id);
Exit when V_test_cursor%notfound;
End Loop;
End

Record variables
Define a record variable using the type command and%rowtype, for more information about%rowstype, see related Materials.
Record variables are used to fetch rows of data from a cursor, and it is much more convenient to use a record than to declare a variable for each column when the cursor selects a number of columns.
When using%rowtype on a table and putting the values taken from the cursor into the record, if you want to select all the columns in the table, using * in the SELECT clause is more than listing all the columns.

Cases:
SET Serveriutput on
DECLARE
R_emp Emp%rowtype;
CURSOR C_emp is a SELECT * from EMP;
BEGIN
OPEN c_emp;
LOOP
FETCH c_emp into r_emp;
EXIT when C_emp%notfound;
Dbms_out. PUT. Put_Line (' Salary of Employee ' | | r_emp.ename| | ' Is ' | | R_emp.salary);
END LOOP;
CLOSE c_emp;
END;

----%type and%rowtype and collections: The record difference can be seen in the following example!

Record Type Example
Create table Studonline--------Student Information sheet
(
Studid Varchar2 (15),
Studname varchar2 (20));


Insert into Studonline (studid,studname)
Values (' 99070470 ', ' Monkey King ');

Insert into Studonline (studid,studname)
Values (' 99070475 ', ' pig ');
DECLARE
TYPE Vipstudrec is RECORD
(Vipid VARCHAR2 (15),
Vipname VARCHAR2 (20));

Studrec Vipstudrec;
BEGIN
SELECT *into Studrec from Studonline WHERE studid= ' 99070470 ';
Dbms_output. Put_Line (Studrec. Vipname);
END;


---select *from studonline;

%type,%rowtype, record difference

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.