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

  • Home
  • SEARCH
  • 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 313481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T08:06:44+00:00 2026-05-12T08:06:44+00:00

First, here are some scripts to setup the tables and background. CREATE TABLE TEST_P

  • 0

First, here are some scripts to setup the tables and background.

CREATE TABLE TEST_P
(
  ID    NUMBER(3) NOT NULL PRIMARY KEY,
  SRC   VARCHAR2(2) NOT NULL,
  DEST  VARCHAR2(2) NOT NULL,
  AMT   NUMBER(4) NOT NULL,
  B_ID_SRC  NUMBER(3),
  B_ID_DEST NUMBER(3)
);

A row in this table indicates that AMT is being moved from SRC to DEST. The ID column is a surrogate key. The first row indicates 10 thingies are being moved from B1 to S1. The values in SRC and DEST are different – the same value can not appear in both.

INSERT INTO TEST_P VALUES (1, 'B1', 'S1', 10, NULL, NULL);
INSERT INTO TEST_P VALUES (2, 'B2', 'S1', 20, NULL, NULL);
INSERT INTO TEST_P VALUES (3, 'B3', 'S2', 40, NULL, NULL);
INSERT INTO TEST_P VALUES (4, 'B1', 'S2', 80, NULL, NULL);
INSERT INTO TEST_P VALUES (5, 'B4', 'S2', 160,NULL, NULL);

There is another table like this. It has a different view of the same information. Each row here indicates something added or removed from “Who”. Values in WHO are B1, B2.. and S1, S2…

CREATE TABLE TEST_B
(
  ID       NUMBER(3)   NOT NULL  PRIMARY KEY,
  BATCH    NUMBER(3)   NOT NULL,
  WHO      VARCHAR2(2) NOT NULL,
  AMT      NUMBER(4)   NOT NULL
);

CREATE SEQUENCE TEST_B_SEQ START WITH 100;

In need to write a process that will periodically take values from TEST_P and populate TEST_B. It must also update
B_ID_SRC and B_ID_DEST which are foreign keys into TEST_B.

Here is my solution so far.

Step 1:

INSERT INTO TEST_B
(ID, BATCH, WHO, AMT)
SELECT TEST_B_SEQ.NEXTVAL, 42, WHO, AMT FROM
(
  SELECT SRC AS WHO, SUM(AMT) AMT FROM TEST_P
  WHERE B_ID_SRC IS NULL AND B_ID_DEST IS NULL
  GROUP BY SRC
  UNION ALL
  SELECT DEST, -SUM(AMT) FROM TEST_P
  WHERE B_ID_SRC IS NULL AND B_ID_DEST IS NULL
  GROUP BY DEST)
;

Step 2:

UPDATE TEST_P
  SET B_ID_SRC = (SELECT ID FROM TEST_B WHERE BATCH = 42 AND TEST_P.SRC = WHO),
      B_ID_DEST = (SELECT ID FROM TEST_B WHERE BATCH = 42 AND TEST_P.DEST = WHO);

There are two problems with this:

1) The rows in the SELECT should be locked. How can I do this select with a FOR UPDATE?

2) If a row is inserted by another session and commited at Step 1.5 then the UPDATE will catch more rows than the INSERT. How should I resolve this without reverting to row by row processing?

Further Details
The real TEST_P table has a status column on it. It’s only when things are in the correct status that they are included into TEST_B.

For various reasons TEST_B is actually required. I can’t just make it a view or something. There is subsequent processing, etc.

  • 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-12T08:06:44+00:00Added an answer on May 12, 2026 at 8:06 am

    in your exemple you’re going to update all rows of TEST_P. Two simple solutions would allow you to be sure that the information on both tables is consistent. You could either:

    1. LOCK TABLE test_p IN EXCLUSIVE MODE for the duration of your transaction (the other inserting sessions would have to wait) or
    2. ALTER SESSION SET ISOLATION_LEVEL=SERIALIZABLE this would prevent the first session to see the changes made by other sessions after the beginning of the transaction.

    Method 1 is straightforward, I will demonstrate method 2:

    session 1> ALTER SESSION SET ISOLATION_LEVEL=SERIALIZABLE;
    
    Session altered
    
    session 1> INSERT INTO TEST_B
            2  (ID, BATCH, WHO, AMT)
            3  SELECT TEST_B_SEQ.NEXTVAL, 42, WHO, AMT FROM
            4  (
            5    SELECT SRC AS WHO, SUM(AMT) AMT FROM TEST_P
            6    WHERE B_ID_SRC IS NULL AND B_ID_DEST IS NULL
            7    GROUP BY SRC
            8    UNION ALL
            9    SELECT DEST, -SUM(AMT) FROM TEST_P
           10    WHERE B_ID_SRC IS NULL AND B_ID_DEST IS NULL
           11    GROUP BY DEST)
           12  ;
    
    6 rows inserted
    

    Here I insert a row with another session and commit:

    session 2> INSERT INTO TEST_P VALUES (6, 'B4', 'S2', 2000,NULL, NULL);
    
    1 row inserted
    
    session 2> commit;
    
    Commit complete
    

    Session 1 does not see the row inserted with session 2 yet:

    session 1> select * from TEST_P;
    
      ID SRC DEST   AMT B_ID_SRC B_ID_DEST
    ---- --- ---- ----- -------- ---------
       1 B1  S1      10          
       2 B2  S1      20          
       3 B3  S2      40          
       4 B1  S2      80          
       5 B4  S2      16
    
    session 1> UPDATE TEST_P
            2    SET B_ID_SRC = (SELECT ID FROM TEST_B WHERE BATCH = 42 AND TEST_P.SRC = WHO),
            3        B_ID_DEST = (SELECT ID FROM TEST_B WHERE BATCH = 42 AND TEST_P.DEST = WHO);
    
    5 rows updated
    
    session 1> commit;
    
    Commit complete
    

    The result is consistent, after the commit session 1 will see the row inserted by session 2:

    session 1> select * from TEST_P;
    
      ID SRC DEST   AMT B_ID_SRC B_ID_DEST
    ---- --- ---- ----- -------- ---------
       6 B4  S2    2000          
       1 B1  S1      10      100       104
       2 B2  S1      20      101       104
       3 B3  S2      40      102       105
       4 B1  S2      80      100       105
       5 B4  S2     160      103       105
    
    6 rows selected
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 184k
  • Answers 184k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Er - all you'd need to do is feed the… May 12, 2026 at 4:53 pm
  • Editorial Team
    Editorial Team added an answer According to the ANSI SQL-92 standard, an UPDATE on JOINed… May 12, 2026 at 4:53 pm
  • Editorial Team
    Editorial Team added an answer I see two tests, one to test that an asychronous… May 12, 2026 at 4:53 pm

Related Questions

Im pretty new to CI so bear with me here. I have just setup
I am just wondering what is the difference between these two File1.js $(function() {
I want my Python script to be able to read Unicode command line arguments
So I'm trying to write a php SOAP client that requires the user to

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.