【问题标题】:How to know which records cause an issue when I run a SQL MERGE statement in Python当我在 Python 中运行 SQL MERGE 语句时,如何知道哪些记录会导致问题
【发布时间】:2021-08-14 01:13:14
【问题描述】:

我在 python 中运行下面的代码。它正在从一个表合并到另一个表。但有时由于重复,它给了我错误。我如何知道哪些记录已合并,哪些未合并,以便我可以跟踪记录并修复它。或者至少,如何让我的代码日志提示错误消息以便我可以跟踪它?

# Exact match client on NAME/DOB (not yet using name_dob_v)
    sql = """
    merge into nf.es es using (
        select id, name_last, name_first, dob 
        from fd.emp
        where name_last is not null and name_first is not null and dob is not null
    ) es6
    on (upper(es.patient_last_name) = upper(es6.name_last) and upper(es.patient_first_name) = upper(es6.name_first)
        and es.patient_dob = ems6.dob)
    when matched then update set 
        es.client_id = ems6.id
      , es.client_id_comment = '2 exact name/exact dob match'
    where
           es.client_id is null -- exclude those already matched
       and es.patient_last_name is not null and es.patient_first_name is not null and es.patient_dob is not null 
       
       and es.is_lock = 'Locked' and es.is_active = 'Yes' and es.patient_last_name NOT IN ('DOE','UNKNOWN','DELETE', 'CANCEL','CANCELLED','CXL','REFUSED')
    """
        log.info(sql)
        curs.execute(sql)
        msg = "nf.es rows updated with es6 client_id due to exact name/dob match: %d" % curs.rowcount
        log.info(msg)
        emailer.append(msg)

【问题讨论】:

    标签: python sql oracle


    【解决方案1】:

    你不知道,merge 不会告诉你的。您必须真正找到他们并采取适当的行动。

    也许选择不同的值会有所帮助:

     merge into nf.es es using (
            select DISTINCT                       --> this
              id, name_last, name_first, dob 
            from fd.emp
            ...
    

    如果它仍然不起作用,则将表与using 子句中的表合并到您已经在执行的所有列上,并查看哪些行是重复的。像这样的:

      SELECT *
        FROM (SELECT d.id,
                     d.name_last,
                     d.name_first,
                     d.dob
                FROM fd.emp d
                     JOIN nf.es e
                        ON     UPPER (e.patient_last_name) = UPPER (d.name_last)
                           AND UPPER (e.patient_first_name) = UPPER (d.name_first)
               WHERE     d.name_last IS NOT NULL
                     AND d.name_first IS NOT NULL
                     AND d.dob IS NOT NULL)
    GROUP BY id,
             name_last,
             name_first,
             dob
      HAVING COUNT (*) > 2;
    

    【讨论】:

    • 啊,是的 - 我编辑了答案(添加了更多信息 - 我希望有用),但忘了告诉你,所以我现在正在做。看看有没有帮助。
    • 谢谢!很有帮助!
    猜你喜欢
    • 2012-06-09
    • 2014-03-08
    • 2017-06-30
    • 1970-01-01
    • 2010-11-28
    • 2019-04-02
    • 2011-04-08
    • 2023-03-05
    • 2021-07-06
    相关资源
    最近更新 更多