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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T17:56:14+00:00 2026-05-20T17:56:14+00:00

I would like to combine insert/update with a case statement, meaning that I want

  • 0

I would like to combine insert/update with a case statement, meaning that I want to insert the row if it doesnt exist, update it if it does, but in both cases with different values (when updating it depends on the id) and when inserting, well then I set it…

And this should happen in a single SQL statement 🙂

In a nutshell: I am trying to combine these two statements into a single one:

1). Insert/Update:

MERGE INTO table_name USING dual ON (id='{id}')
WHEN MATCHED     THEN UPDATE SET {col1}='{val1}', {col2}={val2}
WHEN NOT MATCHED THEN INSERT ({id}, {col1}, {col2}) VALUES ('{id}', '{val1}', {val2})

2). Update different values with case:

UPDATE SIGNALVALUE
    SET
        SIGNUMVALUE = CASE SIGID
        WHEN 49634 THEN 1.1
        WHEN 49674 THEN 2.2
        WHEN 49675 THEN 1.8
        END,
        UPDATETIME = CASE SIGID
        WHEN 49634 THEN TO_TIMESTAMP_TZ('2011-03-18 18:24:56:00', 'YYYY-MM-DD HH24:MI:SS:FF6 TZR')
        WHEN 49674 THEN TO_TIMESTAMP_TZ('2011-03-18 18:24:56:00', 'YYYY-MM-DD HH24:MI:SS:FF6 TZR')
        WHEN 49675 THEN TO_TIMESTAMP_TZ('2011-03-18 18:24:56:00', 'YYYY-MM-DD HH24:MI:SS:FF6 TZR')
        END
   WHERE SIGID IN (49634, 49674, 49675)

These statements are just examples. In reality there are many more rows to insert/update and this happens frequently so I am trying to minimize the amount of queries.

Also I cannot do update and then see how many rows were affected and then insert the ones that werent because I will not know the IDs of the rows that need to be inserted vs the ones that need to be updated. At least as far as I understand…please correct me!

  • 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-20T17:56:14+00:00Added an answer on May 20, 2026 at 5:56 pm

    It doesn’t have to be a table in the using clause, you can also use a SQL query.
    At least that is what I think you are after 🙂

    merge 
     into target_table
    using (select case when ... then ... else ... end as id
             from dual
          ) source_table
        on(target_table.id = source_table.id)
    when matched then
       update 
          set ...
    when not matched then
       insert (...)
       values (...)
    

    Let me know if that helps. If so, I can probably help write the final query as well.

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

Sidebar

Related Questions

I would like to combine 2 commands into 1, if its possible. <%= render
I have two regualar rexpressions I would like to combine into the one for
I have several tables in my ERD which which I would like to combine
If I have a list of strings, I would like to combine them into
I have a field in my bean that I would like to be either
Working with MYSQL in C#: I have a collection of parameterized IDbCommands I would
I'm trying to produce a Winforms report in my .NET application. Our users want
I'm generating xml from tables with queries like this SELECT * FROM Rp FOR
My question has been flagged as a possible duplicate of this question: How to
My C# .NET 2.0 application performs two queries using the ManagementObjectSearcher class: _searcher =

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.