How to update/insert records based on where clause in oracle?

by elisha_langworth , in category: MySQL , 2 months ago

How to update/insert records based on where clause in oracle?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by lizzie , 2 months ago

@elisha_langworth 

To update or insert records based on a WHERE clause in Oracle, you can use the MERGE statement. The MERGE statement allows you to merge data from a source table to a target table based on a specified condition.


Here is an example of how you can use the MERGE statement to update or insert records based on a WHERE clause in Oracle:

1
2
3
4
5
6
7
8
9
MERGE INTO target_table tt
USING source_table st
ON (tt.id = st.id)
WHEN MATCHED THEN
  UPDATE SET tt.column1 = st.column1, tt.column2 = st.column2
WHEN NOT MATCHED THEN
  INSERT (id, column1, column2)
  VALUES (st.id, st.column1, st.column2)
WHERE st.id = 123;


In this example:

  • target_table is the table where you want to update or insert records.
  • source_table is the table from which you want to merge data.
  • id is the common key used to match records between the target and source tables.
  • column1 and column2 are the columns that you want to update in the target table.
  • The WHERE clause specifies the condition based on which the records will be updated or inserted.


You can modify the MERGE statement according to your specific requirements and conditions to update or insert records based on a WHERE clause in Oracle.