Oracle conditional data insertion

Source: Internet
Author: User
Method 1: declareiExistsint; beginselectcount (*) intoiExistsfrom table where condition; ifiExists0theninsertinto table (...) values (...); endif; end; declare the iExists variable and use the condition to find the number of duplicates in the table. If not, insert the data into the table in Method 2:

Method 1: declare iExists int; begin select count (*) into iExists from table where condition; if iExists = 0 then insert into table (...) values (...); end if; end; declare the iExists variable and use the condition to find the number of duplicates in the table. if not, insert the data into the table in Method 2:

Method 1:

Declare
IExists int;
Begin
Select count (*) into iExists from table where condition;
If iExists = 0 then
Insert into table (...) values (...);
End if;
End;

Declare the iExists variable and find the number of duplicates in the table through the conditions. If no, insert the data into the table.

Method 2:

Merge into target table
Using source table | (select statement)
On (condition)
When matched then update set column = Value
Delete where (column = value)
When not matched then insert (column...) values (value ...);

Use the merge method to update or delete matched data, and insert data that does not match

Oracle does not support the insert into... not exists Method

---------------------------------------------------------------------

MSSQLServer method:

If not exists (select * from table where condition...) insert into table (column...) values (value ...);

Or

Declare @ iExists int
Select @ iExists = COUNT (*) from table where condition ...;
If @ iExists = 0
Begin
Insert into table (column...) values (value ...);
End;

----------------------------------------------------------------------------

Note: The insert sequence is different and the result will be affected (Oracle and MSSQL are different)

1,
Merge into student
Using (select * from dual)
On (student. stno = 1 and (student. stname = 'aaa' or student. stname is null ))
When not matched then
Insert (stno, stname, birth)
Values (1, 'aaa', sysdate );

2,
Merge into student
Using (select * from dual)
On (student. stno = 1 and (student. stname = ''or student. stname is null ))
When not matched then
Insert (stno, stname, birth)
Values (1, '', sysdate );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.