Redshift存储过程未正确传递日期

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

[我注意到Redshift将我的日期更改为'2001'。运行下面的一个非常清晰的简单示例

Result3正确是今天的日期(即2019-11-07)。但是,Result2(存储过程)仅返回“ 2001”。我尝试过投射以及以不同的方式传递日期,但结果保持不变。有什么建议吗?


CREATE OR REPLACE PROCEDURE pub.test_sp1(IN param date,INOUT tmp_name varchar(256))
AS $$
DECLARE 
  row record;
BEGIN
EXECUTE 'drop table if exists ' || tmp_name;
EXECUTE 'create temp table ' || tmp_name || ' as
select ' || param;
END;
$$ LANGUAGE plpgsql;

CALL pub.test_sp1(current_date, 'myresult');
SELECT * from myresult;

select current_date
date stored-procedures amazon-redshift
1个回答
0
投票
EXECUTE 'create temp table ' || tmp_name || ' as select ' || param;

被转换(在连接字符串并替换值之后)为>

EXECUTE 'create temp table myresult as select 2019-11-07'; -- => select 2001

不要这样连接值,请使用quote_ident()quote_literal()正确引用:

EXECUTE 'create temp table ' || quote_ident(tmp_name) || ' as select ' || quote_literal(param);

或使用format()功能:

EXECUTE format('create %I as select %L', tmp_date, param);

例如:

#= SELECT 'test: ' || current_date as incorrect, 
          'test: ' || quote_literal(current_date) as correct;
┌──────────────────┬────────────────────┐
│    incorrect     │      correct       │
├──────────────────┼────────────────────┤
│ test: 2019-11-07 │ test: '2019-11-07' │
└──────────────────┴────────────────────┘
© www.soinside.com 2019 - 2024. All rights reserved.