How to Use % Type and % rowtype in Oracle PL/SQL

Source: Internet
Author: User

How to Use % Type and % rowtype in Oracle PL/SQL

1. Using % Type in many cases, PL/SQL variables can be used to store data in database tables. In this case, the variable should have the same type as the table column. For example, if the first_name column type of the Students table is varchar2 (20), we can declare a variable as follows: declare v_firstname varchar2 (20 ); but what will happen if the definition of the first_name column is changed (for example, if the table is changed, the current type of first_name is changed to varchar2 (25 ))? All PL/SQL code that uses this column must be modified. If you have a lot of PL/SQL code, such processing may be time-consuming and error-prone. In this case, you can use the "% Type" attribute instead of hard encoding the variable type. Example: declare
V_firstname students. first_name % type; Use the % Type and v_firstname variables to set the same type as the first_name columns in the students table ). This type is determined every time an anonymous block or nameblock runs the statement block and compiles stored objects (processes, functions, packages, object classes, and triggers. Using % type is a good programming style because it makes PL/SQL more flexible and more suitable for updating database definitions.

 

2. Use % rowtype 2.1 PL/SQL to record PL/SQL record types similar to the structure in C language. It is a composite type and user-defined. Record provides a mechanism for processing independent variables, but also as a whole unit-related variable. See declare v_studentid number (5); v_firstname varchar2 (20); v_lastname varchar2 (20); these three variables are logically correlated, because they direct to different fields in the students table. If a record type is declared for these variables, the relationship between them is obvious and can be processed 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 you can assign values to records using the SELECT statement. This will retrieve data from the database and store the data in records. Note that the fields in the record should match those in the query result list. Select
Studentid, firstname, lastname into v_studentinfo from students where studentid = 32; 2.3 it is common to declare a record as a database row of the same type in PL/SQL using % rowtype. PL/SQL provides the % rowtype operator to facilitate such operations. For example, declare v_roomrecord rooms % rowtype; a record is defined, and the fields in the record correspond to columns in the rooms table.

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.