Oracle SQL |合并 - 选择 - 更新条款|目标表的子集

时间:2017-09-14 12:10:55

标签: sql oracle merge

我打算做什么?
要为匹配的目标记录的子集更新一些目标表字段,请在源和目标之间进行匹配,前提是它们都是同一个表。

我需要帮助的地方?
Merge-into- 目标 -using-select- -on-子句似乎只能控制两个类别 -

  • 当源和目标记录匹配时,更新所有匹配的目标记录
  • 当源和目标记录不匹配时,插入或执行其他控制操作。

就我而言,当源和目标记录匹配时,我想对并非所有目标记录执行更新,但要在目标记录子集上执行

请求帮助以指导此事。

此处提供参考代码(目标过滤器限定符已注释,因为它肯定无法正常工作):

MERGE INTO TGT
USING (
SELECT id, account_number, part_role, inc_val, pay_frequency, 
period, pay_option, inclusion_value 
FROM Table_1
WHERE id = var1
AND account_number = var2
AND part_role in ('YY','XX')
) SRC
ON (
SRC.id = TGT.id
AND SRC.account_number = TGT.account_number
--TGT.part_role in ('AA','BB','CC','DD','EE') --This must be a wrong place for this qualifier, please help on where to include this qualifier for filtering out target records for update purpose
)
WHEN MATCHED THEN
UPDATE SET
TGT.inc_val = SRC.inc_val 
,TGT.pay_frequency = SRC.pay_frequency 
,TGT.period = SRC.period 
,TGT.pay_option = SRC.pay_option 
,TGT.inclusion_value = SRC.inclusion_value;

其他相关详情
数据库:Oracle
版本:11gR2
环境:Unix

1 个答案:

答案 0 :(得分:2)

where语句后添加SET子句。

MERGE INTO TGT
USING (SELECT
  id,
  account_number,
  part_role,
  inc_val,
  pay_frequency,
  period,
  pay_option,
  inclusion_value
FROM Table_1
WHERE id = var1
AND account_number = var2
AND part_role IN ('YY', 'XX')) SRC
ON (
SRC.id = TGT.id
AND SRC.account_number = TGT.account_number
--TGT.part_role in ('AA','BB','CC','DD','EE') --This must be a wrong place for this qualifier, please help on where to include this qualifier for filtering out target records for update purpose
)
WHEN MATCHED THEN
UPDATE SET
TGT.inc_val = SRC.inc_val
, TGT.pay_frequency = SRC.pay_frequency
, TGT.period = SRC.period
, TGT.pay_option = SRC.pay_option
, TGT.inclusion_value = SRC.inclusion_value
WHERE TGT.part_role in ('AA','BB','CC','DD','EE');