如何解决 Redshift 存储过程中无法将类型整数转换为日期的问题?

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

我有一个程序,我正在使用

call
 cross_ref_var_procedure('2023-05-31'::
date
,'2023-05-08'::
Date
,aws_access_key,aws_secret_key).

调用该程序

我收到的错误为

SQL Error [42846]: ERROR: cannot cast type integer to date

WHERE first_day(CAST(effective_date AS DATE)) <= first_day(cast(2023-05-08 as date))AND   first_day(CAST(end_Date AS DATE)) >= first_day(cast(2023-05-08 as date)) ;"
PL/pgSQL function "cross_ref_var_procedure" line 39 at execute statement

如何解决这个问题

create or replace procedure cross_ref_var_procedure
(in cur_date DATE, 
in f_date date,
IN aws_access_key varchar,
IN aws_secret_key varchar)
as
$$
DECLARE
    formatted_date VARCHAR;
    c_date VARCHAR;
begin
------start
--import

formatted_date := TO_CHAR(f_date, 'YYYYMMDD');

c_date := TO_CHAR(cur_date, 'YYYYMMDD');

execute 'create schema raw_raw_dqat_rs_'||c_date||';';

execute 'create table raw_raw_dqat_rs_'||c_date||'.medical_mutual_medical_delimited_5_v03
(SOURCE_PROVIDER_ID varchar(max),
NPI varchar(max),
PROVIDER_TIN varchar(max),
Effective_Date varchar(max),
End_Date varchar(max),
sourcefilename varchar(max));';

execute 'copy raw_raw_dqat_rs_'||c_date||'.medical_mutual_medical_delimited_5_v03(
SOURCE_PROVIDER_ID,
NPI,
PROVIDER_TIN,
Effective_Date,
End_Date
) from ''s3://bucket/filename'’
credentials ''aws_access_key_id='||aws_access_key||';aws_secret_access_key='||aws_secret_key||'''
COMPUPDATE off
ACCEPTINVCHARS
delimiter as ''|'';';

execute 'update raw_raw_dqat_rs_'||c_date||'.medical_mutual_medical_delimited_5_v03
set sourcefilename = ''s3://bucket/filename'’
where sourcefilename is null;';
---selecting active members

execute 'select * into  raw_raw_dqat_rs_'||c_date||'.prv_cross_active_testt
from raw_raw_dqat_rs_'||c_date||'.medical_mutual_medical_delimited_5_v03
WHERE first_day(CAST(effective_date AS DATE)) <= first_day(cast('||f_date||' as date))
AND   first_day(CAST(end_Date AS DATE)) >= first_day(cast('||f_date||' as date)) ;';

---backup
execute 'select * into raw_raw_dqat_rs_'||c_date||'.prv_cross_active_backup
from raw_raw_dqat_rs_'||c_date||'.prv_cross_active;';

--ordering by max eff_date and min source_provider_id
execute 'with w1 as(
select distinct case when dense_rank() over (partition by provider_tin,npi order by effective_date desc,source_provider_id asc) = 1 then ''selected spi'' else '''' 
end as is_selected,*
from  raw_raw_dqat_rs_'||c_date||'.prv_cross_active_backup order by provider_tin,npi
)
select * into raw_raw_dqat_rs_'||c_date||'.prv_cross_active_changed from w1;';

execute 'select * into raw_raw_dqat_rs_'||c_date||'.cross_ref_final
from raw_raw_dqat_rs_20240415.prv_cross_active_changed
where is_selected=''selected spi'';';

execute 'alter table raw_raw_dqat_rs_'||c_date||'.cross_ref_final
drop is_selected;';

execute 'alter table raw_raw_dqat_rs_'||c_date||'.cross_ref_final
drop sourcefilename;';

execute 'unload (''select * from raw_raw_dqat_rs_'||c_date||'.cross_ref_final'')    
to ''s3://dw-da-corawn/TeamA/raw_raw/'||formatted_date||'/crossref/raw_ProviderCrossReference_'||formatted_date||'.txt''
credentials ''aws_access_key_id='||aws_access_key||';aws_secret_access_key='||aws_secret_key||'''
HEADER  
PARALLEL OFF 
ALLOWOVERWRITE
DELIMITER as ''|'';';

END;
$$ LANGUAGE plpgsql;



我尝试了上述解决方案

sql postgresql amazon-web-services stored-procedures amazon-redshift
1个回答
0
投票

您似乎在两个地方犯了类似的语法错误,这是其中之一:

first_day(cast(2023-05-08 as date))

Redshift 中的日期文字应该用单引号引起来,因此您可以使用:

first_day(cast('2023-05-08' as date))

请注意,Redshift 还支持 Postgres 风格的双冒号转换运算符,因此您同样可以使用更简洁的版本:

first_day('2023-05-08'::date)
© www.soinside.com 2019 - 2024. All rights reserved.