Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6929577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:20:08+00:00 2026-05-27T11:20:08+00:00

I have a cursor that queries a table like this CURSOR Cur IS SELECT

  • 0

I have a cursor that queries a table like this

CURSOR Cur IS
       SELECT Emp_No,status
        from Employee
        FOR UPDATE OF status;

Now I would want to update my status in Employee table from another table using the Emp_no. Once I have done this I need to use this status for calling custom business logic and not the original status retrieved by the cursor. What is the best way of going about this? Here is what I have written. I declared a variable called v_status by the way

 FOR Rec IN Cur LOOP

           BEGIN

           UPDATE Employee           
SET status = (select a.status from Employee_Status  where a.Emp_No = rec.Emp_No)
           WHERE CURRENT OF Cur ;
           COMMIT;

           END;

           SELECT status INTO v_status 
           FROM  Employee
           where Emp_No = rec.Emp_No;

            IF(v_status = 'Active') THEN
                   -- Custom Business Logic
                  ELSE  
            -- Business logic

            END IF;    
END LOOP;

What would be a better way to achieve this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T11:20:09+00:00Added an answer on May 27, 2026 at 11:20 am

    1) I’m hoping in your real code that you don’t have a COMMIT in the middle of your loop. Since committing releases the locks held by your transaction, the row-level locks taken out with the FOR UPDATE clause are released and other sessions are free to update the same rows. In later versions of Oracle, you’ll get an “ORA-01002: fetch out of sequence” if you do this. In earlier versions of Oracle, this error was ignored which lead to occasionally incorrect results.

    2) Do you need to update the EMPLOYEE table on a row-by-row basis? I’d tend to move the update outside of the loop in order to maximize SQL since that’s the most efficient way to process data. If your business logic is amenable to it, I’d also suggest doing bulk operations to fetch the data into local collections that your business logic can iterate through in order to minimize context shifts between SQL and PL/SQL.

    So, based on your comments, in your example, there should be a predicate in the definition of your cursor, right? Something like this?

    CURSOR Cur IS
      SELECT Emp_No,status
        from Employee
       WHERE status IS NULL
          FOR UPDATE OF status;
    

    If so, you’d need that same predicate in the single UPDATE statement (potentially instead of the EXISTS clause). But since you know that you only want to process the rows that your UPDATE statement affected, you can just return those rows into a local collection

    DECLARE
      CURSOR cur IS 
         SELECT emp_no, status
           FROM employee
          WHERE status IS NULL;
      TYPE l_employee_array IS
        TABLE OF cur%rowtype;
      l_modified_employees l_employee_array;
    BEGIN
      UPDATE employee e
         SET status = (select es.status
                         from employee_status es
                        where es.emp_no = e.emp_no)
       WHERE status IS NULL
         AND EXISTS (SELECT 1
                       FROM employee_status es
                      WHERE es.emp_no = e.emp_no)
      RETURNING emp_no, status
           BULK COLLECT INTO l_modified_employees;
      FOR i IN l_modified_employees.FIRST .. l_modified_employees.LAST
      LOOP
        IF( l_modified_employees(i).status = 'Active' ) 
        THEN
          -- Custom logic 1
        ELSE
          -- Custom logic 2
        END IF;
      END LOOP;
    END;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the cursor with the query statement as follows: cursor.execute(select rowid from components
I have a function that updates a MySQL table from a CSV file. The
right now I have min, max value from table1 that defines the range of
I'm using MySQLdb and Python. I have some basic queries such as this: c=db.cursor()
I have the following CSS that hides the mouse cursor for anything on the
I have a function in a package that returns a REF CURSOR to a
I have the following function that deletes the LaTeX command surrounding the current cursor
I have two cursors in my procedure that only differ on the table name
I have obtained the crosshair cursor from NSCursor crosshairCursor. Then, how can i change
I have this small table of data. Dir LinkL LinkH East 19 27 East

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.