【问题标题】:Not able to dynamically truncate table in Redshift Stored Procedure无法在 Redshift 存储过程中动态截断表
【发布时间】:2020-01-30 04:41:24
【问题描述】:

我在 Redshift 中有一个表(我们称之为状态表),我在其中设置了要截断的表的状态。为了实现这一目标,我创建了一个 Redshift Stored Procedure。这是我的 SP 代码:

CREATE OR REPLACE PROCEDURE <schema>.truncate_table()
AS $$
DECLARE
    v_tpsl RECORD;
    exec_statement VARCHAR(256);
BEGIN
    FOR v_tpsl in SELECT * from <schama>.tablename_process_status_log WHERE status = 'TRUE' LOOP
    exec_statement = 'TRUNCATE TABLE <schema>.' + quote_ident(v_tpsl.staging_table_name) + '_test;';
    RAISE INFO 'statement = %', exec_statement;
    EXECUTE exec_statement;
  END LOOP;
END;
$$
LANGUAGE plpgsql;

现在当我调用存储过程时,我收到了这个错误: SQL Error [500310] [34000]: [Amazon](500310) Invalid operation: cursor does not exist;

我查看了 SP 的文档以检查 Truncate 是否可行。通过查看examples,看起来这是可能的。

我不确定这出了什么问题。我正在使用 RedshiftJDBC42-no-awssdk-1.2.34.1058.jar 并通过 DBeaver 连接。

【问题讨论】:

  • 尝试使用常规 PostgreSQL 而不是错误的 fork。
  • @LaurenzAlbe - 这是一个很好的建议,但由于我们想使用它,所以不能这样做。

标签: amazon-redshift


【解决方案1】:

看来我找到了答案。根据thisAny cursor that is open (explicitly or implicitly) is closed automatically when a COMMIT, ROLLBACK, or TRUNCATE statement is processed。在我的下一次循环迭代中,它试图访问已经关闭的游标。

【讨论】:

  • 这不是一个很好的答案。您只是在确认问题所在。您采取了哪些措施来解决这个问题?
猜你喜欢
  • 2010-10-12
  • 2022-01-23
  • 1970-01-01
  • 2018-12-18
  • 2012-11-05
  • 2012-03-17
  • 2023-03-21
  • 1970-01-01
相关资源
最近更新 更多