Search code examples
sqloracle-xe

sql update statement - insert data if record does not exist?


I thought the update statement required data to already exist.

I am supporting a java app. The previous developer for some code used a delete statement to clear out the record then used an update statement to insert data back into the table.

Can the update statement be used to insert or update data into a table???

The db is oracle express. that should not make a difference.


Solution

  • You can try using MERGE statement for getting the same result. With MERGE statement you can either insert new records into table or update if the data already exists.

    Please find the syntax of the MERGE statement.

    MERGE INTO <table_name>
    USING <table_view_or_query>
    ON (<condition>)
    WHEN MATCHED THEN <update_clause>
    DELETE <where_clause>
    WHEN NOT MATCHED THEN <insert_clause>;
    

    Perhaps you can give more details about you problem. I am assuming this is what is required by you.