Oracle select for update

Source: Internet
Author: User

Oracle select for update
In most cases, the rows checked by the cursor are modified for the processing completed in the extraction loop. PL/SQL provides a syntax for such processing.

This syntax includes two parts: the for update clause in the cursor declaration part and the where current of clause in the update or delete statement.

Generally, the Select operation does not perform any lock settings on the row being processed, which allows other sessions connected to the database to change the selected data.

However, the result set is consistent. After the activity set is determined, Oracle will take a snapshot of the table when the open operation is executed. Any changes submitted before this time point will be reflected in the active dataset. Any changes made after this time point, even if they have been submitted, will not be reflected unless the cursor is re-opened. However, if you use the for update clause, a mutex lock will be added to the corresponding row of the previous active set returned by the open clause. These locks will prevent other sessions from changing the rows in the active set. Until the entire transaction is committed.

Example:
Declare
Cursor c_cur is select * From studends for update of XM;
Begin
Open c_cur;
While c_cur % found Loop

Update studends set XM = 'A' | XM where current of c_cur;

End loop;
Close c_cur;
Commit;
End;

Note: 1. The update statement only updates the columns listed in the for update clause declared by the cursor. If no column is listed, all columns can be updated.

2. In the example, commit is completed after the extraction cycle is completed, because commit releases all the locks held by the session. Because the for update clause acquires locks, commit releases these locks. When the lock is released, the cursor is invalid. Therefore, Oracle errors will be returned for subsequent extraction operations.

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.