Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle insert if not exists statement

Tags:

oracle

insert into OPT (email, campaign_id) values('[email protected]',100) where not exists( select * from OPT where (email ="[email protected]" and campaign_id =100)) ; 

Error report: SQL Error: ORA-00933: SQL command not properly ended 00933. 00000 - "SQL command not properly ended" *Cause:
*Action:

how to insert a new row if it doesn't exists in Oracle?

like image 852
user1427096 Avatar asked May 30 '12 21:05

user1427096


People also ask

How do you insert if row does not exist in SQL?

There are three ways you can perform an “insert if not exists” query in MySQL: Using the INSERT IGNORE statement. Using the ON DUPLICATE KEY UPDATE clause. Or using the REPLACE statement.

Can we use insert statement in function in Oracle?

The INSERT command can also take the values directly from another table using 'SELECT' statement rather than giving the values for each column. Through 'SELECT' statement, we can insert as many rows as the base table contains. Syntax: BEGIN INSERT INTO <table_name>(<column1 >,<column2>,...

How can I insert values from one table to another in Oracle?

The simplest way to create an Oracle INSERT query to list the values using the VALUES keyword. For example: INSERT INTO suppliers (supplier_id, supplier_name) VALUES (5000, 'Apple'); This Oracle INSERT statement would result in one record being inserted into the suppliers table.


2 Answers

insert into OPT (email, campaign_id)  select '[email protected]',100 from dual where not exists(select *                   from OPT                   where (email ='[email protected]' and campaign_id =100)); 
like image 54
a_horse_with_no_name Avatar answered Sep 19 '22 09:09

a_horse_with_no_name


The correct way to insert something (in Oracle) based on another record already existing is by using the MERGE statement.

Please note that this question has already been answered here on SO:

  • oracle insert if row not exists
  • insert if not exists oracle
like image 28
p.marino Avatar answered Sep 18 '22 09:09

p.marino