Merge Data into IDR.PRESC_TPL Table Using Merge Statement
merge into idr.PRESC_TPL T1
using (
<foreach collection="prescList" item="item" separator="union">
select
#{item.prescTplId} as PRESC_TPL_ID,
#{item.dataVersion} as DATA_VERSION,
#{item.hospitalId} as HOSPITAL_ID,
#{item.tplName} as TPL_NAME,
#{item.sortId} as SORT_ID,
#{item.createDeptId} as CREATE_DEPT_ID,
#{item.createPersonId} as CREATE_PERSON_ID,
#{item.prescType} as PRESC_TYPE,
#{item.prescDiag} as PRESC_DIAG,
#{item.drugDept} as DRUG_DEPT,
#{item.fromType} as FROM_TYPE,
#{item.prescDiagId} as PRESC_DIAG_ID,
#{item.prescCategory} as PRESC_CATEGORY,
#{item.parentTplId} as PARENT_TPL_ID,
#{item.keyWordOne} as KEY_WORD_ONE,
#{item.keyWordTwo} as KEY_WORD_TWO,
#{item.quasiDiagnosisId} as QUASI_DIAGNOSIS_ID
from dual
</foreach>
) T2
on (T1.PRESC_TPL_ID = T2.PRESC_TPL_ID)
when matched then
update set
gmt_modify = CURRENT_TIMESTAMP,
DATA_VERSION = T1.DATA_VERSION + 1,
HOSPITAL_ID = T2.HOSPITAL_ID,
TPL_NAME = T2.TPL_NAME,
SORT_ID = T2.SORT_ID,
CREATE_DEPT_ID = T2.CREATE_DEPT_ID,
CREATE_PERSON_ID = T2.CREATE_PERSON_ID,
PRESC_TYPE = T2.PRESC_TYPE,
PRESC_DIAG = T2.PRESC_DIAG,
DRUG_DEPT = T2.DRUG_DEPT,
FROM_TYPE = T2.FROM_TYPE,
PRESC_DIAG_ID = T2.PRESC_DIAG_ID,
PRESC_CATEGORY = T2.PRESC_CATEGORY,
PARENT_TPL_ID = T2.PARENT_TPL_ID,KEY_WORD_ONE = T2.KEY_WORD_ONE,KEY_WORD_TWO = T2.KEY_WORD_TWO,
QUASI_DIAGNOSIS_ID = T2.QUASI_DIAGNOSIS_ID
when not matched then
insert (
PRESC_TPL_ID, DATA_VERSION, GMT_CREATE, GMT_MODIFY, HOSPITAL_ID, TPL_NAME, SORT_ID, CREATE_DEPT_ID,
CREATE_PERSON_ID, PRESC_TYPE, PRESC_DIAG, DRUG_DEPT, FROM_TYPE, PRESC_DIAG_ID, PRESC_CATEGORY,
DEL_FLAG, PARENT_TPL_ID,KEY_WORD_ONE , KEY_WORD_TWO,QUASI_DIAGNOSIS_ID
)
values (
T2.PRESC_TPL_ID, 1,sysdate, sysdate, T2.HOSPITAL_ID, T2.TPL_NAME,
T2.SORT_ID, T2.CREATE_DEPT_ID, T2.CREATE_PERSON_ID, T2.PRESC_TYPE, T2.PRESC_DIAG,
T2.DRUG_DEPT, T2.FROM_TYPE, T2.PRESC_DIAG_ID, T2.PRESC_CATEGORY, '0', T2.PARENT_TPL_ID,
T2.KEY_WORD_ONE,T2.KEY_WORD_TWO, T2.QUASI_DIAGNOSIS_ID
)
Explanation:
- Merge Statement: This SQL statement combines both
UPDATEandINSERToperations based on matching conditions. - Target Table:
idr.PRESC_TPLis the target table where data will be updated or inserted. - Source Data: The
usingclause defines the source data for the merge operation. It uses aforeachloop to iterate over a collection namedprescListand dynamically generates aselectstatement for each item in the collection. - Matching Condition: The
onclause specifies the condition for matching rows between the target table (T1) and the source data (T2). In this case, it uses thePRESC_TPL_IDcolumn. - Update Clause: The
when matched thenclause defines the update operation if a match is found between the target table and the source data. It updates various columns based on the corresponding values in the source data, includingDATA_VERSION,HOSPITAL_ID,TPL_NAME, etc. - Insert Clause: The
when not matched thenclause defines the insert operation if no match is found. It inserts a new row into the target table with values from the source data. TheDEL_FLAGcolumn might need additional attention as it is not explicitly set in theselectstatement. Also, theDATA_VERSIONfor the newly inserted row is set to1, which might need adjustment based on the application's requirements.
Note: The provided code snippet is a general example. It might require further adjustments depending on the actual data types, constraints, and business logic of the specific database table and application.
原文地址: https://www.cveoy.top/t/topic/qCFe 著作权归作者所有。请勿转载和采集!