Oracle cursor deletion and update operations

Source: Internet
Author: User
Tags oracle cursor

The following articles mainly introduce Oracle cursor deletion and UPDATE. We all know that in PL/SQL, UPDATE and DELETE statements can be used to UPDATE or DELETE related data rows. An explicit Oracle cursor is used only when multiple rows of data are required.

PL/SQL allows you to delete or update records only using Oracle cursors.

The where current of substring in the UPDATE or DELETE statement is used to process the most recent data retrieved from the table for the UPDATE or DELETE operation.

To use this method, you must use the for update substring when declaring the cursor. When you use the for update substring to open an Oracle cursor, all data rows in the return set will be in ROW-LEVEL) Exclusive locking. Other objects can only query these data rows and cannot perform UPDATE, DELETE, or SELECT... for update operation.

Syntax:

 
 
  1. FOR UPDATE [OF [schema.]table.column[,[schema.]table.column]..  
  2. [nowait]  

In multi-table queries, the "OF" clause is used to lock a specific table. If the "OF" clause is ignored, all selected data rows in the table are locked. If these data rows have been locked by other sessions, ORACLE will normally wait until the data row is unlocked.

The syntax for using where current of substring in UPDATE and DELETE is as follows:

 
 
  1. WHERE{CURRENT OF cursor_name|search_condition}  

Example:

 
 
  1. DELCARE  
  2. CURSOR c1 IS SELECT empno,salary  
  3. FROM emp  
  4. WHERE comm IS NULL  
  5. FOR UPDATE OF comm;  
  6. v_comm NUMBER(10,2);  
  7. BEGIN  
  8. FOR r1 IN c1 LOOP  
  9. IF r1.salary<500 THEN  
  10. v_comm:=r1.salary*0.25;  
  11. ELSEIF r1.salary<1000 THEN  
  12. v_comm:=r1.salary*0.20;  
  13. ELSEIF r1.salary<3000 THEN  
  14. v_comm:=r1.salary*0.15;  
  15. ELSE  
  16. v_comm:=r1.salary*0.12;  
  17. END IF;  
  18. UPDATE emp;  
  19. SET comm=v_comm 
  20. WHERE CURRENT OF c1l;  
  21. END LOOP;  
  22. END   

The above content is an introduction to the update and deletion of Oracle cursors. I hope you will find some gains.

Article by: http://www.programbbs.com/doc/class10-3.htm

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.