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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:39:59+00:00 2026-06-18T12:39:59+00:00

Below is my table in which ID column is Primary Key . And other

  • 0

Below is my table in which ID column is Primary Key. And other two columns are string.

I am using a new Database named XpressMP.

Column Name                
-------
ID             PrimaryKey                 
SEARCHES       String
ACCOUNT        String

I am trying to achieve functionality of UPSERT here-

If ID doesn't exists here then insert a new record.
And ID exists then update the record.

I know If I am working with Oracle then I can use MERGE sql command but MERGE is not supported in that database and there is no other command for that as of now. But I believe I can do the same thing with Stored Procedure.

Can anyone provide some suggestions how can I do the same thing with Stored Procedure? As stored procedure will work there.

UPDATED:-

public final static String INSERT = "BEGIN"
+" INSERT INTO TABLE (ID, SEARCHES, ACCOUNT) VALUES (?, ?, ?)"
+" EXCEPTION"
+" WHEN DUP_VAL_ON_INDEX THEN"
+" UPDATE TABLE"
+" SET SEARCHES = ?, ACCOUNT = ?"
+" WHERE ID = ?"
+" END";

Whenever I try to execute the above stored procedure like this

preparedStatement = dbConnection.prepareStatement(INSERT);

preparedStatement.setString(1, String.valueOf(userId));
preparedStatement.setString(2, Constants.getaAccount(userId));
preparedStatement.setString(3, Constants.getaAdvertising(userId));
preparedStatement.executeUpdate();

I get exception? Is this the right way to execute 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-06-18T12:40:00+00:00Added an answer on June 18, 2026 at 12:40 pm

    You can handle it using the DUP_VAL_ON_INDEX exception.
    You can add the below code in your procedure, which should meet your requirement.

    CREATE OR REPLACE PROCEDURE TABLE_UPSERT (v_id IN NUMBER,
                                              v_searches IN VARCHAR2(20),
                                              v_account IN VARCHAR2(20)) AS
    BEGIN
      INSERT INTO table (id, searches, account) VALUES (v_id, v_searches, v_account) ;
    EXCEPTION
      WHEN DUP_VAL_ON_INDEX THEN
        UPDATE TABLE
        SET searches = v_searches, account = v_account
        WHERE id = v_id;
    END;
    /
    

    You can read more about DUP_VAL_ON_INDEX Exception here and here.


    Another option would be to insert or update the data after checking the count.

    DECLARE
      l_count number;
    BEGIN
      SELECT count(*) 
      INTO l_count
      FROM table 
      WHERE id = (value);
    
      IF l_count = 0 THEN
        INSERT into table VALUES ....;
      ELSE
        UPDATE table SET searches = .., account = ..;
      END IF;
    END;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table which has the below columns. Ticket_id (Primary key, Int) Attachment1
I have two table masterTbl and detailTbl which structure is given below... --PRIMARY TABLE
I m new with jquery.Below is my html table which is in content page.In
I have the following listbox below which binds to a database table of image
I have two tables as below: Student: oneID (primary key), col1, col2, col3, subId,
How should columns in a database table that have a PK/FK relationship be named?
I am designin a new table which has an identity column with int type.The
The table I am working on has a primary key which is essentially a
I have a table with a primary key and row key column. The data
I have a table pictured below that has a surrogate Key as the Primary

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.