【问题标题】:Redshift Stored Procedures not passing dates properlyRedshift 存储过程未正确传递日期
【发布时间】:2020-03-04 12:31:43
【问题描述】:

我注意到 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

【问题讨论】:

  • 它在做数学:2019 - 11 - 7 = 2001

标签: date stored-procedures amazon-redshift


【解决方案1】:
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' │
└──────────────────┴────────────────────┘

【讨论】:

  • 这修复了它!非常感谢!
猜你喜欢
  • 2012-05-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-23
  • 2014-07-27
  • 2017-04-22
  • 2019-11-02
相关资源
最近更新 更多