How to insert conditionally in oracle?

Member

by dana , in category: MySQL , 23 days ago

How to insert conditionally in oracle?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by adan , 22 days ago

@dana 

In Oracle, you can insert data conditionally using a combination of the INSERT INTO statement and the CASE expression within the VALUES clause. Here's an example:

1
2
3
4
5
6
INSERT INTO table_name (column1, column2, column3)
VALUES (
    CASE WHEN condition1 THEN value1 ELSE default_value END,
    CASE WHEN condition2 THEN value2 ELSE default_value END,
    CASE WHEN condition3 THEN value3 ELSE default_value END
);


In the above example, you would replace table_name with the name of the table you are inserting data into and column1, column2, and column3 with the names of the columns you are inserting data into.


You can specify different conditions for each column by using the CASE expression within each VALUE statement. If the condition evaluates to true, the specified value will be inserted into the respective column. If the condition evaluates to false, the default_value will be inserted instead.


You can have multiple conditions and values for each column as needed. Just make sure to adjust the number of columns and values accordingly.