当匹配更新不工作oracle合并

时间:2013-11-03 11:03:14

标签: sql oracle insert-update on-duplicate-key

这是有趣的事情! merge语句工作正常。但是当值匹配时,它不会将状态更新为' B'正如所料。知道为什么吗?

merge into bbbt b 
using (select bank,ifsc,branch,
              center,district,state,
              address,status 
         from rtgbrn_temp 
        where ifsc in (SELECT ifsc 
                         FROM rtgbrn_temp 
                        MINUS 
                       SELECT ifsc_code 
                         FROM bbbt)
        ) x
   on (x.ifsc = b.ifsc_code)
 when matched then 
   update 
      set rtgs_stat = 'B' 
    where (x.ifsc=b.ifsc_code)
 when not matched then 
   insert (bankid,benbrn_code,brn_name,
           brn_addr,brn_loc,brn_stat,
           brn_city,coun_code,
           remarks,ifsc_code,
           rtgs_stat)
   values (substr(x.ifsc,1,4), substr(x.ifsc,5), 
           x.branch, x.address, x.district, 
           x.state, x.center,'IN', x.bank, x.ifsc,'R');

[也尝试使用on duplicate update,但无法处理错误]

SQL Error: ORA-00933: SQL command not properly ended!

insert into bbbt(bankid,benbrn_code,brn_name,brn_addr,brn_loc,brn_stat,brn_city,coun_code,remarks,ifsc_code,rtgs_stat)
select substr(x.ifsc,1,4), substr(x.ifsc,5), x.branch, x.address, x.district, x.state, x.center,'IN', x.bank, x.ifsc,'R' from 
(select bank,ifsc,branch,center,district,state,address,status from rtgbrn_temp where ifsc in (SELECT ifsc FROM rtgbrn_temp MINUS SELECT ifsc_code FROM bbbt))x
ON DUPLICATE KEY UPDATE rtgs_stat = 'B'; 

请分享您的知识,谢谢!

2 个答案:

答案 0 :(得分:2)

查询中的值无法匹配
仔细查看这部分查询。

select ..., ifsc, .....
from rtgbrn_temp 
where ifsc in (........
               .........
               MINUS 
               SELECT ifsc_code 
               FROM bbbt )

此查询检索<{1}}表格中不等于ifsc代码。{
1}} 然后查询使用这些代码来匹配记录:

ifsc_code

以上与

等效
bbbt

因为 on (x.ifsc = b.ifsc_code) 是子查询的别名,on (subquery.ifsc = bbbt.ifsc_code) x表名。

想一会儿......
子查询仅返回不等于b的任何值的代码 这意味着条件bbbt 永远不会匹配

答案 1 :(得分:0)

编辑代码:

merge into bbbt b using (select bank,ifsc,branch,center,district,state,address,status from rtgbrn_temp) x
on (x.ifsc = b.ifsc_code)
when matched then update set rtgs_stat = 'B'
when not matched then insert (bankid,benbrn_code,brn_name,brn_addr,brn_loc,brn_stat,brn_city,coun_code,remarks,ifsc_code,rtgs_stat)
values (substr(x.ifsc,1,4), substr(x.ifsc,5), x.branch, x.address, x.district, x.state, x.center,'IN', x.bank, x.ifsc,'R');