代码之家  ›  专栏  ›  技术社区  ›  Rikesh Kayastha

如何解决错误:运算符不唯一:动态SQL查询中的“未知”-“未知”?

  •  1
  • Rikesh Kayastha  · 技术社区  · 2 年前

    我在存储过程中有一个执行块,我正在动态执行查询:

    execute 'create table raw_mine.financial_multicase_xwalk_' || target_date || '
    as
    select distinct
      a.cvr_mnth_dt
    , a.filler_string_10
    , a.alt_prsn_id
    , a.member_id
    from raw_mine.rstag_mine_TO_CGT_MBRSHP_' || target_date || ' a
    left join
    raw_mine.rstag_mine_TO_CGT_FIN_' || target_date || ' b
    on a.filler_string_10 = b.filler_string_10
    and a.member_id = b.member_id
    and left ( a.cvr_mnth_dt, 6) = left ( b.cvr_mnth_dt, 6)
    left join
    raw_mine.rstag_mine_TO_CGT_FIN_' || target_date || ' cp
    on a.filler_string_10 = cp.filler_string_10
    and a.alt_prsn_id = cp.alt_prsn_id
    and left ( a.cvr_mnth_dt, 6) = left ( cp.cvr_mnth_dt, 6)
    left join
    raw_mine.member_xwalk_dulality_' || target_date || ' c
    on a.alt_prsn_id = c.alt_prsn_id
    and left ( a.cvr_mnth_dt, 6) = left ( replace ( c.cvr_mnth_dt, '-', ''), 6)
    where nullif ( c.alt_prsn_id, '') is null
    and cp.alt_prsn_id is not null
    and cp.member_id != a.member_id;';
    

    所有列都是varchars和 target_date 是过程的输入参数,我得到一个错误

    SQL错误[42725]:错误:运算符不唯一:“未知”-“未知”

    提示:无法选择最佳候选运算符。您可能需要添加显式类型强制转换。

    知道怎么解决这个问题吗?

    此查询在过程外部执行时有效。

    1 回复  |  直到 2 年前
        1
  •  1
  •   user23781552    2 年前

    您似乎忘记了转义以下行中定义字符串的单引号:

    and left ( a.cvr_mnth_dt, 6) = left ( replace ( c.cvr_mnth_dt, '-', ''), 6)
    where nullif ( c.alt_prsn_id, '') is null
    

    你可能想要

    and left ( a.cvr_mnth_dt, 6) = left ( replace ( c.cvr_mnth_dt, ''-'', ''''), 6)
    where nullif ( c.alt_prsn_id, '''') is null