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 7410331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:13:07+00:00 2026-05-29T06:13:07+00:00

I made a cursor in order to merge/update records in the database I was

  • 0

I made a cursor in order to merge/update records in the database

I was wondering if it is correct or if anyone has any suggestions in order to improve the query.

DECLARE
CURSOR c_itemloc
IS
SELECT 
    item ,
    loc ,
    loc_type ,
    source_method ,
    primary_supp ,
    source_wh
FROM
  (SELECT dc_vert.item ,
    dc_vert.loc ,
    dc_vert.loc_type ,
    dc_vert.source_method ,
    dc_vert.primary_supp ,
    w.primary_vwh source_wh --,dc_vert.source_wh
    ,
    dc_vert.actie ,
    MAX(dc_vert.actie) over (PARTITION BY dc_vert.item, dc_vert.loc) actie_max ,
    COUNT(dc_vert.primary_supp) over (PARTITION BY dc_vert.item, dc_vert.loc) primary_count
  FROM dc_item_loc_pim_lms dc_vert ,
    item_supplier isu ,
    store sto ,
    wh w
  WHERE dc_vert.primary_supp    IS NOT NULL
  AND isu.item                   = dc_vert.item
  AND dc_vert.primary_supp       = isu.supplier
  AND W.WH                       = dc_vert.source_wh
  AND sto.store                  = dc_vert.loc
  AND ISU.SUPP_DISCONTINUE_DATE >= SYSDATE
  )
  WHERE actie       = actie_max
  AND primary_count = 1;
  l_item item_loc.item%TYPE;
  l_loc item_loc.loc%TYPE;
  loc_type item_loc.loc_type%TYPE;
  l_source_method item_loc.source_method%TYPE;
  l_primary_supp item_loc.primary_supp%TYPE;
  l_source_wh item_loc.source_wh%TYPE;

  i        NUMBER;
  l_commit VARCHAR2(1) := 'Y';
  BEGIN
  i             :=0;
  FOR r_itemloc IN c_itemloc
  LOOP
  i := i+1;
  UPDATE item_loc il
  SET 
    il.source_method        = r_itemloc.source_method ,     -- 'S'
    il.loc_type             = r_itemloc.loc_type ,      -- 'S'
    il.primary_supp         = r_itemloc.primary_supp ,
    il.source_wh            = r_itemloc.source_wh ,
    il.last_update_datetime = SYSDATE
  WHERE item                    = r_itemloc.item
  AND   loc                   = r_itemloc.loc;
  IF l_commit                   = 'Y' AND mod(i, 1000) = 0 THEN
  COMMIT ;
  END IF;
  END LOOP;
  EXCEPTION
  WHEN OTHERS THEN
     dbms_output.put_line('SOMETHING WENT WRONG');
  END;
  • 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-29T06:13:07+00:00Added an answer on May 29, 2026 at 6:13 am

    1) You’re replacing a meaningful error with a meaningless one. Plus, if you have haven’t set output on, an error will be missed entirely. The best thing to do would be to simply remove the exception block. If you can’t do that, you should dump at least SQLERRM and probably DBMS_UTILITY.FORMAT_ERROR_BACKTRACE to DBMS_OUTPUT. To catch and record errors well, you’d need to pass the exception details to a separate procedure that would use an autonomous transaction to write those details to a table. Though, even in that case, you’re best off re-raising the error after you record it.

    2) Committing every X records is a poor practice in most cases. If these records are all being updated together, then they should be part of the same transaction.

    3) You could do this in a single statement using either UPDATE or MERGE. Generally that’s preferred, as it avoids additional context switches. Personally, I like MERGE in this scenario:

    MERGE INTO item_loc il
    USING      (SELECT item,
                       loc,
                       loc_type,
                       source_method,
                       primary_supp,
                       source_wh
                FROM   (SELECT dc_vert.item,
                               dc_vert.loc,
                               dc_vert.loc_type,
                               dc_vert.source_method,
                               dc_vert.primary_supp,
                               w.primary_vwh source_wh,
                               dc_vert.actie,
                               MAX(dc_vert.actie) OVER (PARTITION BY dc_vert.item, dc_vert.loc) actie_max,
                               COUNT(dc_vert.primary_supp) OVER (PARTITION BY dc_vert.item, dc_vert.loc) primary_count
                        FROM   dc_item_loc_pim_lms dc_vert,
                               item_supplier isu,
                               store sto,
                               wh w
                        WHERE  dc_vert.primary_supp IS NOT NULL
                           AND isu.item = dc_vert.item
                           AND dc_vert.primary_supp = isu.supplier
                           AND w.wh = dc_vert.source_wh
                           AND sto.store = dc_vert.loc
                           AND isu.supp_discontinue_date >= SYSDATE)
                WHERE  actie = actie_max AND primary_count = 1) itemloc
    ON         (il.item = itemloc.item AND il.loc = itemloc.loc)
    WHEN MATCHED THEN
       UPDATE SET
          il.source_method          = itemloc.source_method,
          il.loc_type               = itemloc.loc_type,
          il.primary_supp           = itemloc.primary_supp,
          il.source_wh              = itemloc.source_wh,
          il.last_update_datetime   = SYSDATE;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have made the following code to retrive data from SQLite database. public Cursor
I have made a MatrixCursor for accessing my data. It has 4 columns. And
I've made it so that when you hover the cursor over the image button
Cursor cursor = resolver.query( Data.CONTENT_URI, DataQuery.PROJECTION, DataQuery.SELECTION, new String[] {String.valueOf(rawContactId)}, null); With PROJECTION being:
I've made database design for a small CRM system. It comprises of Companies and
i have made a few multi activity database applications using sqlite, but i get
I've a QGraphicsView with an image and a custom cursor. The cursor is made
Made this nice little loop for hiding and showing div's, works as a charm
Made a custom obj called Item with some string fields and one float. .h
I made a function which displays date on the webpage,, and i uploaded the

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.