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

问题描述 投票:0回答:1

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

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 Error [42725]: ERROR: operator is not unique: "unknown" - "unknown"
  Hint: Could not choose a best candidate operator. You may need to add explicit type casts.

知道如何解决这个问题。

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

sql stored-procedures amazon-redshift plpgsql dynamic-sql
1个回答
0
投票

看起来您忘记了转义这些行中定义字符串的单引号:

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
© www.soinside.com 2019 - 2024. All rights reserved.