Basic PL/SQL programming (iii) [custom composite variables] [Application of % TYPE and % ROWTYPE]

Source: Internet
Author: User

1. Custom composite variable query and output case:
Declare
TYPE myrecord is record (
Aa varchar2 (10 ),
Bb varchar2 (10); -- defines compound Variables
Record1 myrecord;
Begin
Select a1, a2 into record1 from a where a2 = 'mmm'; -- assign a value to the variable
Dbms_output.put_line (record1.aa | record1.bb); -- Query Output Method
End;
/
 
 
2. In the case of the % type application, the difference from the previous case is that changing the basic type to the % type of the corresponding table will automatically match, as shown in the following case:
Declare
TYPE recorde is record (
Id emp. eid % type, -- Use % type to define compound data [Table name]. [column name] % type
Name emp. ename % type,
Age emp. eage % type
);
Record1 recorde;
Begin
Select eid, ename, eage into record1 from emp where eid = '08'; -- assign a value
Dbms_output.put_line ('Id: '| record1.id); -- output
Dbms_output.put_line ('name: '| record1.name );
Dbms_output.put_line ('Age: '| record1.age );
End;
/
 
 
3. In the application case of % rowtype, unlike the previous one, each sub-variable record of the composite variable is not declared. We can directly declare it with [Table name % rowtype. As follows:
Declare
Myrec emp % rowtype; -- Use % rowtype to declare a composite variable
Begin
Select * into myrec from emp where eid = '010 ';
Dbms_output.put_line ('myrec. eid' | myrec. eid );
Dbms_output.put_line ('myrec. ename' | myrec. ename );
Dbms_output.put_line ('myrec. eage' | myrec. eage );
End;
/

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.