The last row of plsql cursor is duplicated.

Source: Internet
Author: User

The last row of plsql cursor is duplicated.
Let's take a closer look at the first Stored Procedure test01. Is there a problem? It seems that there is no problem, but it will actually cause repeated rows. test02 puts the exit when statement in the appropriate position. The last row will not be printed repeatedly.

Create or replace procedure test01 as cursor cursor1 is select * from v $ session where rownum <= 5; record1 cursor1 % rowtype; begin DBMS_OUTPUT.ENABLE (buffer_size => null); open cursor1; loop fetch cursor1 into record1; dbms_output.put_line (record1.sid); exit when cursor1 % notfound; end loop; close cursor1; end;
Using create or replace procedure test02 as cursor cursor1 is select * from v $ session where rownum <= 5; record1 cursor1 % rowtype; begin DBMS_OUTPUT.ENABLE (buffer_size => null); open cursor1; loop fetch cursor1 into record1; exit when cursor1 % notfound; dbms_output.put_line (record1.sid); end loop; close cursor1; end;



In plsql, If I define a cursor, then open the cursor, and then insert all the queried data into the self-defined table by loop.

Open will not waste too much time. The time required is two parts.
1. Define the cursor when obtaining the result set. --- This time mainly depends on how long your SQL query takes.
2. Loop. ---- This is the running time.

However, we recommend that you do not use a cursor to process large volumes of data.
The processing speed of the cursor is very slow. Low efficiency. It is better to do batch processing.

I used to process data with cursors. My business logic is complicated and I can only process hundreds of thousands of data every hour. The efficiency is too low. Later I changed to multiple steps and used insert into select .. From can be processed by a hundred or eighty times faster.

SQL Server uses a cursor during storage. The query result does not contain the first data, but the number of cycles is correct. However, the last two data entries are duplicated.

Set fetch next from mycursor into @ I, @ name
Put it at the end of the while LOOP body, that is:
While (@ fetch_status = 0)
Begin
Print 'id: '+ @ I + 'name:' + @ name
Fetch next from mycursor into @ I, @ name
End
This should be done.

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.