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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T10:49:31+00:00 2026-05-21T10:49:31+00:00

Let’s say that I have a table of items, and for each item, there

  • 0

Let’s say that I have a table of items, and for each item, there can be additional information stored for it, which goes into a second table. The additional information is referenced by a FK in the first table, which can be NULL (if the item doesn’t have additional info).

TABLE item (
    ...
    item_addtl_info_id INTEGER
)

CONSTRAINT fk_item_addtl_info FOREIGN KEY (item_addtl_info)
    REFERENCES addtl_info (addtl_info_id)

TABLE addtl_info (
    addtl_info_id INTEGER NOT NULL
    GENERATED BY DEFAULT 
    AS IDENTITY (
        INCREMENT BY 1
        NO CACHE
        ),
    addtl_info_text VARCHAR(100)
    ...
    CONSTRAINT pk_addtl_info PRIMARY KEY (addtl_info_id)
)

What is the “best practice” to update an item’s additional info (in IBM DB2 SQL, preferably)?

It should be an UPSERT operation, meaning that if additional info does not yet exist then a new record is created in the second table, but if it does, then it is only updated, and the FK in the first table does not change.

So imperatively, this is the logic:

UPSERT(item, item_info):
CASE WHEN item.item_addtl_info_id IS NULL THEN
    INSERT INTO addtl_info (item_info)
    UPDATE item.item_addtl_info_id (addtl_info.addtl_info_id)
                                               ^^^^^^^^^^^^^
ELSE
    UPDATE addtl_info (item_info)
END

My main problem is how to get the newly inserted addtl_info row’s id (underlined above). In a stored proc I can request the id from a sequence and store it in a variable, but maybe there is a more straightforward way. Isn’t it something that comes up all the time when programming databases?

I mean, I’m really not interested in what the id of the addtl_info record is as long as it remains unique and is referenced properly. So using sequences seems a bit of an overkill to me in this case.

As a matter of fact, this UPSERT operation should be part of the SQL language as a standard operation (maybe it is, and I just don’t know about it?)…

  • 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-21T10:49:32+00:00Added an answer on May 21, 2026 at 10:49 am

    The syntax I was looking for is:

    SELECT * FROM NEW TABLE ( INSERT INTO phone_book VALUES ( 'Peter Doe','555-2323' ) )
    

    from Wikipedia (http://en.wikipedia.org/wiki/Insert_%28SQL%29)

    This is how to refer to the record that was just inserted in the table.

    My colleague called this construct an “in-place trigger”, which what it really is…

    Here is the first version that I put together as a compound SQL statement:

    begin atomic
    declare addtl_id integer;
    set addtl_id = (select item_addtl_info_id from item where item.item_id = XXX);
    if addtl_id is null
    then
        set addtl_id = (select addtl_info_id from new table 
                        (insert into addtl_info
                            (addtl_info_text)
                            values ('My brand new additional info')
                        )
                      );
        update item set item.item_addtl_info_id = addtl_id
        where item.item_id = XXX;
    
    else
       update addtl_info set addtl_info_text = 'My updated additional info'
       where addtl_info.addtl_info_id = addtl_id;
    end if;
    end
    

    XXX being equal to the item id to be updated – this code can now be easily inserted into a sproc, and XXX can be converted to an input parameter.

    I also tried using MERGE INTO, but I couldn’t figure out a syntax for updating a table different from what was specified as the target.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let me explain best with an example. Say you have node class that can
Let's say I have a table with a Color column. Color can have various
Let's say I have an abstract parent class called shape, and that there are
Let's say I have a dataset, which can be neatly classified using weka's J48
Let's say I have a posts table that has many comments (and a comment
Let's say I have a method in java, which looks up a user in
Let's say that I have a SQLite database that I create in a separate
Let's say I have two text files that I need to extract data out
Let's say for a moment that I have the following module in python: class
Let's say you have a class library project that has any number of supplemental

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.