Oracle deletes data according to Rownum

Source: Internet
Author: User

Recently, we have been porting SQL to Oracle. Since Oracle does not have the same Identity column as in SQL, we have encountered a lot of trouble, recently I encountered a stored procedure for deleting data based on the values of the auto-incrementing column. I found a method for half a day.

  1. /* Procedure in Oracle */
  2. -- Create a table. Because there is no identity in Oracle, remove the aid column and use rownum later.
  3. Create table TempTable (
  4. SearchID number (10, 0)
  5. )
  6. -- Delete the value of Rownum 5
  7. Declare cursor tmp_cursorIsSelect rownum aid, searchid from TempTableForUpdate;
  8. Tmp_record tmp_cursor % rowtype;
  9. Begin
  10. Open tmp_cursor;
  11. Loop
  12. Fetch tmp_cursor into tmp_record;
  13. Exit when tmp_cursor % notfound;
  14. If(Tmp_record.aid = 5) -- If rownum is 5
  15. Then
  16. Begin
  17. Delete TempTable where current of tmp_cursor;
  18. End;
  19. EndIf;
  20. End loop;
  21. Close tmp_cursor;
  22. Commit;
  23. 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.