合并和源表多个匹配

时间:2013-09-23 13:16:42

标签: sql oracle

我遇到以下问题的问题:

merge into table2 d
using (
  select firstname, lastname, max(id) id
  from table1 t1
  group by firstname, lastname
  having count(0) = 1
) s
on (d.firstname=s.firstname and d.lastname=s.lastname)
when matched then update set t1_id = s.id;

如果table2中的多行与ON子句匹配,那么我得到“SQL错误:ORA-30926:无法在源表中获得一组稳定的行”

你知道过滤器的任何方法,只是忽略那些“重复”吗? 感谢。

修改

@Polppan,您对样本数据的请求以一种非常奇怪的方式引导我:

这里有一些示例数据:

table1
ID       firstname     lastname
1        John          Doe
2        John          DOE
3        Jane          Doe
4        Jane          Doe

(注意UPPER)

table2
t1_ID    firstname     lastname
null     John          Doe
null     Jane          Doe
null     Jane          Doe

现在,我无法使用这些数据重现错误,直到:

  • ON子句是“UPPER(d.firstname)= UPPER(s.firstname)AND UPPER(d.lastname)= UPPER(s.lastname)”(这是我所需要的,因为我需要不区分大小写的匹配)
  • table1中的一行具有大写的DOE

知道为什么吗?

1 个答案:

答案 0 :(得分:1)

尝试使用DISTINCT

MERGE INTO   table2 d
      USING  (SELECT     DISTINCT ((firstname))fname, ((lastname))lname,max(id) id
                        FROM     table1 t1
                        GROUP BY   firstname, lastname
                     HAVING  COUNT (0) = 1
                  ) s
          ON     ( upper(d.firstname) = upper(fname)
                  AND upper(lastname) = upper(lname))
WHEN MATCHED
THEN
    UPDATE SET id = s.id;

更新1

MERGE INTO   table2 d
      USING  (SELECT     DISTINCT upper((firstname))fname, upper((lastname))lname,max(id) id
                        FROM     table1 t1
                        GROUP BY   firstname, lastname
                     HAVING  COUNT (0) = 1
                  ) s
          ON     ( upper(d.firstname) = upper(fname)
                  AND upper(lastname) = upper(lname))
WHEN MATCHED
THEN
    UPDATE SET id = s.id;