Get the App
SLTechnology News&Howtos  ›  Database  › 

Jdbc uses the merge into function in oracle

Shulou Source: shulou.com Published: 2022-06-01 11:32:10 10月03日 Update

Merge into can merge data already in the oracle data table. If there is, update, otherwise update

The following example passes the test after the jdbc data source is connected

1. Single data

Long companyAuthId = SeqkeyUtil.getSeqKey (vids, "COMPANY_AUTH_ID"); CompanyAuth comtmp = companyList.get (0); String sql = "merge into COMPANY_AUTH auth using (select? MOBILE from dual) tmp on (auth.mobile=tmp.mobile)" + "WHEN MATCHED THEN" + "UPDATE SET auth.reserve=?" + "WHEN NOT MATCHED THEN" + "INSERT (id,MOBILE) VALUES"; vids.update (sql, new Object [] {comtmp.getMobile (), comtmp.getReserve (), companyAuthId+ ", comtmp.getMobile ()})

2. Multiple pieces of data (batch processing)

Long companyAuthId = SeqkeyUtil.getSeqKey (vids, "COMPANY_AUTH_ID"); / / seqs table primary key uses List batch = new ArrayList (); for (CompanyAuth company: companyList) {Object [] values = new Object [] {company.getMobile (), company.getReserve (), companyAuthId+ ", company.getMobile ()}; batch.add (values);} String sql =" merge into COMPANY_AUTH auth using (select? MOBILE from dual) tmp on (auth.mobile=tmp.mobile) "+" WHEN MATCHED THEN "+" UPDATE SET auth.reserve=? "+" WHEN NOT MATCHED THEN "+" INSERT (id,MOBILE) VALUES "; vids.batchUpdate (sql, batch)

If more than one table appears, you only need to target using (select? MOBILE from dual) tmp on can be modified.

Tags: Data can make multiple multiple data sources data tables is in examples updates tests functions Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Docker MariaDB MySQL macOS