如果表中的行不存在,请向上插入-Oracle

时间:2019-04-22 05:33:40

标签: sql oracle

我想使用merge语句更新存在的行,或者如果不存在则在Table中插入新行。可以使用MERGE完成

MERGE into BANK_DETAILS A using ( SELECT * FROM BANK_DETAILS ) B ON (A.BANKID=<<>> AND A.COUNTRY=<<>>) 
WHEN MATCHED THEN 
UPDATE SET A.STATUS=<<condiition>>, A.IS_BLOCKED=<<>> , A.IS_DISABLED=<<>>,A.LOGO=<<>>,a.BANK_GROUP=<<>>,A.LAST_MODIFIED_DATE=<<>>,A.LAST_MODIFIED_BY=<<>>
WHEN NOT MATCHED THEN
INSERT (A.ID,A.BANKID,A.COUNTRY,A.BANKNAME,A.LOGO,A.STATUS,A.IS_BLOCKED,A.IS_DISABLED,A.BANK_GROUP,A.CREATED_DATE,A.CREATED_BY,A.LAST_MODIFIED_DATE,A.LAST_MODIFIED_BY) 
VALUES (BANK_DETAILS_ID_SEQ.NEXTVAL,?,?,?,?,?,?,?,?,sysdate,?,sysdate,?)
;

1 个答案:

答案 0 :(得分:0)

是的,只需从dual子句中的USING中选择值,而不是bank_details

MERGE INTO bank_details a USING ( SELECT 2345 as bankid,
                                         'India' as country, 
                                          'SBI' as bankname,
                                           ..
                                           ..
                                           ...
                                      FROM dual ) b ON ( .. )
  ..
  ..
  UPDATE SET A.status= b.status, a.is_blocked = b.is_blocked,..
  ...
  ...
 VALUES
   ( bank_details_id_seq.NEXTVAL,b.bankid,b.country,?,?,?,?,?,?,
      SYSDATE,?,SYSDATE,? );