Oracle uses bulkcollect to implement SQL statements for batch fetch of cursor

Source: Internet
Author: User
Oracle uses bulkcollect to implement SQL statements for batch fetch of cursor. You can try it with friend El.

Oracle uses bulk collect to implement SQL statements for batch fetch of cursor. You can try it with friend El.

In general, there are not many chances to use fetch in batches, but we 'd better get familiar with this feature provided by Oracle, and maybe it will be used at any time.


The Code is as follows:
Declare
Cursor c1 is select * from t_depart;
V_depart t_depart % rowtype;
Type v_code_type is table of t_0000.depart_code % type;
V_code v_code_type;
Type v_name_type is table of t_0000.depart_name % type;
V_name v_name_type;
Begin
Open c1;
Fetch c1 bulk collect into v_code, v_name;
For I in 1 .. v_code.count loop
Dbms_output.put_line (v_code (I) | ''| v_name (I ));
End loop;
Close c1;
End;

Through the above column, you can find that if there are many columns, defining a set for each column seems complicated. You can combine the set with % rowtype to use the simplified program!
The Code is as follows:
Declare
Cursor c1 is select * from t_depart;
Type v_depart_type is table of t_depart % rowtype;
V_depart v_depart_type;
Begin
Open c1;
Fetch c1 bulk collect into v_depart;
For I in 1 .. v_).count loop
Dbms_output.put_line (v_depart (I). depart_code | ''|
V_depart (I). depart_name );
End loop;
Close c1;
End;

When outputting results, you can use both the count attribute of the Set and the first and last attributes. When referencing content of the % rowtype, you must note that v_depart (I ). depart_code, rather than v_pai.depart_code (I), is of course not written in this way, even if it makes sense.
The Code is as follows:
Declare
Cursor c1 is select * from t_depart;
Type v_depart_type is table of t_depart % rowtype;
V_depart v_depart_type;
Begin
Open c1;
Fetch c1 bulk collect into v_depart;
For I in v_mongo.first... v_mongo.last loop
Dbms_output.put_line (v_depart (I). depart_code | ''|
V_depart (I). depart_name );
End loop;
Close c1;
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.