我试图将一个表(table1
)中的某些数据插入到另一个表(table2
)中,就像数据存在时只更新数据或插入数据一样。但问题是现在我面临的是,当我添加最后一个条件,如果first_name喜欢'mss%'我必须插入SERVICE_TYPE_ID 10否则20.但是这种情况并没有发生这种情况并且所有案例条件都成功。所以我会想知道最后一个案例条件有什么问题
trim(b.first_name) like 'mss%'
THEN 10
ELSE 20
这主要是在Merge更新部分失败并且插入20而不是10 这是我的完整oracle查询
MERGE INTO table2 a
USING table1 b ON (a.second_name = b.first_name)
WHEN MATCHED THEN
UPDATE
SET a.friendly_name = TRIM(b.friendly_name),
a.account_number = TRIM(b.account_no),
a.ne_type = TRIM(b.ci_type),
a.service_type_id = CASE
WHEN upper(trim(b.company_name)) LIKE '%MWAN%'
OR upper(trim(b.company_name)) LIKE '%MGWAN%'
OR upper(trim(b.company_name)) LIKE '%MWLAN%'
OR upper(trim(b.company_name)) LIKE '%MLAN%'
OR upper(trim(b.company_name)) LIKE '%DDOS%'
OR trim(b.first_name) LIKE 'mss%'
THEN 10 ELSE 20 END,
a.to_locate_in_gis ='Y'
WHERE a.account_number <> b.account_no
WHEN NOT MATCHED THEN
INSERT (ne_slno, second_name, company_id, ne_type, ne_status, service_type_id)
VALUES (s_node_seq_nextval,
TRIM(b.first_name), TRIM(b.company_id),
TRIM(b.ci_type), TRIM(b.ci_status),
CASE
WHEN upper(trim(b.company_name)) LIKE '%MWAN%'
OR upper(trim(b.company_name)) LIKE '%MGWAN%'
OR upper(trim(b.company_name)) LIKE '%MWLAN%'
OR upper(trim(b.company_name)) LIKE '%MLAN%'
OR upper(trim(b.company_name)) LIKE '%DDOS%'
OR trim(b.first_name) LIKE 'mss%'
THEN 10 ELSE 20 END)
WHERE SUBSTR(b.first_name, 1, 1) != ' '
除了指向trim(b.first_name) like 'mss%'
答案 0 :(得分:2)
在您的插入中,案例陈述中的最后一个表达式为trim(b.first_name) like 'mss%'
。
但是,插入部分merge语句的谓词为:WHERE SUBSTR(b.first_name, 1, 1) != ' ')
,它排除了mss
之前有空格的所有行。
这可能是导致问题的原因。我建议您从插入部分中删除该谓词,或者将or trim(b.first_name) like 'mss%'
添加到where子句中。