oracle merge into

/*Merge into Details
MERGE statement is a new syntax of Oracle9i, used to merge UPDATE and INSERT statements.
Through the MERGE statement, query another table according to the join condition of a table or subquery,
UPDATE if the join condition matches, and execute INSERT if the join condition matches.
This syntax only needs a full table scan to complete all the work, and the execution efficiency is higher than INSERT+UPDATE.
*/
/* Syntax:
MERGE [INTO [schema .] table [t_alias]
USING [schema .] { table | view | subquery } [t_alias]
ON ( condition )
WHEN MATCHED THEN merge_update_clause
WHEN NOT MATCHED THEN merge_insert_clause;
*/

Syntax:

MERGE INTO [your table-name] [rename your table here]

USING ( [write your query here] )[rename your query-sql and using just like a table]

ON ([conditional expression here] AND [...]. ..)

WHEN MATHED THEN [here you can execute some update sql or something else ]

WHEN NOT MATHED THEN [execute something else here ! ]


/*
We still take the table in "Case Application in SQL" as an example. Creating another two tables fzq1 and fzq2
*/ --all
male records
create table fzq1 as select * from fzq where sex=1; --all
female records
create table fzq2 as select * from fzq where sex=0;
/*involved An example of association between two tables */
--Update table fzq1 so that the chengji field in the records with the same id +1, and update the name field.
--If the id is not the same, insert it into the table fzq1.
--Add the grade recorded by the boy in the fzq1 table +1, and insert the girl into the table fzq1
merge into fzq1 aa --The fzq1 table is the table that needs to be updated
using fzq bb -- Association table
on (aa.id=bb.id) --association condition
when matched then --match association condition, update processing
update set
aa.chengji=bb.chengji+1,
aa.name=bb.name -- This is just to show that multiple fields can be updated at the same time.
when not matched then -- does not match the associated conditions, for insertion processing. If only for update, the following statement can be omitted.
insert values( bb.id, bb.name, bb.sex,bb.kecheng,bb.chengji); --You
can query the fzq1 table by yourself.
/*Involving an example of association of multiple tables, we take three tables as an example, only for update processing, not for insertion processing. Of course, you can also only do insert processing*/
--Add 1 to the grades recorded by girls in the fzq1 table, without going directly to the sex field. Rather, fzq and fzq2 are associated.
merge into fzq1 aa --fzq1 table is the table that needs to be updated
using (select fzq.id,fzq.chengji
       from fzq join fzq2
       on fzq.id=fzq2.id) bb -- data set
on (aa.id=bb.id ) --Association condition
when matched then --Match the association condition and do update processing
update set
aa.chengji=bb.chengji+1 --You
can query the fzq1 table by yourself.
/* Can't do things*/
merge into fzq1 aa   
using fzq bb          
on (aa.id=bb.id)       
when matched then      
update set
aa.id=bb.id+1
/*System prompt:
ORA-38104: Columns referenced in the ON Clause cannot be updated: "AA"."ID"
We can't update fields in on (aa.id=bb.id) association condition */
update fzq1
set id=(select id+1 from fzq where fzq.id=fzq1.id)
where id in
(select id from fzq)
--Use update to update, if there is a better way, thank you for your feedback!


<update id="updateAcctSpecBalForCashMod" parameterType="com.pinganfu.acct.common.dal.model.AcctAccountSpecBalanceDO">
merge into T_ACCT_SPEC_ACCOUNT t
using (select #{acctId,jdbcType=VARCHAR} as acct_id,
              #{specCode,jdbcType=VARCHAR } as spec_code,
              #{curType,jdbcType=VARCHAR} as cur_type,
              #{specialBalance,jdbcType=DECIMAL} as balance
         from dual) a
on (t.acct_id = a.acct_id and t.spec_code = a.spec_code)
when matched then
  update set t.balance = a.balance,
             t.modify_time = systimestamp
when not matched then
  insert(acct_id,spec_code,balance,create_time)
  values(a.acct_id,a.spec_code,a.balance,systimestamp)
</update>

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327071290&siteId=291194637