而不是更新我的DBA要求我删除然后将值插回到表中以获得性能。我需要在这里使用merge。 我之前没有合作过。我该如何继续这样做?下面给出的是删除和插入查询。可以合并用于删除和单独插入而不进行任何更新吗?
DELETE FROM hist_tab1 hist
WHERE hist.tranid IN (SELECT t.tranid
FROM transaction_tab t
WHERE t.tran_status =
'Posted');
INSERT INTO hist_tab1 (tranid,
tranamt,
date,
post_date,
isvalid,
salescode)
(SELECT tnr.tranid,
tranamt,
date,
post_date,
isvalid,
salescode
FROM tab1 tnr,
(SELECT tranid
FROM transaction_tab t
WHERE t.tran_statu IN ('Posted','Verified')) tt
WHERE tnr.tranid = tt.transid);
答案 0 :(得分:0)
删除适用于更新。但它只会删除目标表更改的行并满足where子句。 否则你可以使用一些虚拟列并尝试更新并将删除子句放在下面。
e.g。
Merge into hist_tab1 src using
((SELECT tnr.tranid,
tranamt,
date,
post_date,
isvalid,
salescode
FROM tab1 tnr,
(SELECT tranid
FROM transaction_tab t
WHERE t.tran_statu IN ('Posted','Verified')) tt
on tnr.tranid = src.transid
when matched then update src.dummy = tt.dummy
delete where tt.tranid IN (SELECT t.tranid
FROM transaction_tab t
WHERE t.tran_status =
'Posted')
when not matched INSERT (src.tranid,
src.tranamt,
src.date,
src.post_date,
src.isvalid)
values
(trg.tranid,
trg.tranamt,
trg.date,
trg.post_date,
trg.isvalid
)
) salescode)