Use of % TYPE and % ROWTYPE in ORACLE

Source: Internet
Author: User

Use of % TYPE and % ROWTYPE in ORACLE
1% TYPE description

To make the data TYPE of a variable consistent with that of another defined variable (especially a column in a table), Oracle provides the % TYPE definition method. When the data type of the referenced variable changes, the Data Type of the newly defined variable automatically follows the change, which is easy to be consistent and does not need to be modified by the PL/SQL program. When you cannot accurately know the Data Type of the referenced variable, you can only use this method to define the data type of the variable.

2% ROWTYPE description

If a table has many columns, use % ROWTYPE to define a variable that represents a record in a row of the table, which is much simpler than using % TYPE to define the variables of each column in the table, it is not easy to omit or make mistakes. This will increase the maintainability of the program.

To make the data type of a variable consistent with the data type of each column recorded in a table, Oracle provides the % ROWTYPE definition method. When the data type of some columns in the table changes, the Data Type of the newly defined variable automatically follows the change, which is easy to be consistent and does not need to be modified by the PL/SQL program. When you cannot know the structure and data type of the referenced table, you can only use this method to define the data type of the variable.

A row of records can store the column data of the entire data row queried from a table or cursor. Each column in a record has the same name and data type as each column in a table.

3. Example 3.1 Data Preparation
-- Create table SF_ORG (ORG_ID int not null, -- Organization primary key IDORG_NAME VARCHAR2 (50), -- Organization Name PARENT_ID INT -- Organization parent level) -- insert into SF_ORG (ORG_ID, ORG_NAME, PARENT_ID) VALUES (1, 'level 1 Department 1', 0); -- level 2 Department insert into SF_ORG (ORG_ID, ORG_NAME, PARENT_ID) VALUES (2, 'second-level department 2', 1); insert into SF_ORG (ORG_ID, ORG_NAME, PARENT_ID) VALUES (3, 'second-level department 3', 1 ); insert into SF_ORG (ORG_ID, ORG_NAME, PARENT_ID) VALUES (4, 'level 2 Department 4', 1 );
3.2% TYPE

Declare two variables with the same data type as the PARENT_ID and ORG_NAME columns in the SF_ORG table, and then use the replace variable & ORG_ID to accept the input Organization Code, query and display the Organization Name and parent department ID. Note: When using a variable defined by % TYPE, use the "." operator to specify the table name qualifier.

Shows the execution result:

The SQL statement in type01.txt is as follows:

DECLARE V_ORG_NAME BEGIN % TYPE; -- same as ORG_NAME V_PARENT_ID SF_ORG.PARENT_ID % TYPE; -- same as PARENT_ID begin select ORG_NAME, PARENT_ID INTO V_ORG_NAME, V_PARENT_ID FROM SF_ORG so where so. ORG_ID = & ORG_ID; DBMS_OUTPUT.PUT_LINE ('department name: '| V_ORG_NAME); DBMS_OUTPUT.PUT_LINE ('superior department code:' | TO_CHAR (V_PARENT_ID); END;
1.1.3.3% ROWTYPE

Declare the row record variable V_SF_ORG_REC, which has the same column name and data type as in the SF_ORG table, and then use the replace variable & ORG_ID to accept the input Organization Code, query and display the Organization Name and parent department ID. Note: When using a variable defined by % ROWTYPE, use the "." operator to specify a variable name qualified word.

Shows the execution result:

The SQL statement in rowtype01.txt is as follows:

DECLARE V_SF_ORG_REC SF_ORG % ROWTYPE; -- same as columns in the SF_ORG table, begin select * INTO V_SF_ORG_REC FROM SF_ORG so where so. ORG_ID = & ORG_ID; DBMS_OUTPUT.PUT_LINE ('department ID: '| TO_CHAR (bytes); DBMS_OUTPUT.PUT_LINE ('department name:' | bytes); DBMS_OUTPUT.PUT_LINE ('superior department code: '| TO_CHAR (V_SF_ORG_REC.PARENT_ID); END;

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.