在 Redshift 返回错误的存储过程中传递变量

Passing variable inside stored procedure in Redshift returning error

我有这个可以成功编译的存储过程,但是在调用该过程时出现错误:

Amazon Invalid operation: syntax error at or near ""

你能帮我理解哪里出了问题吗?提前谢谢你。

 CREATE OR REPLACE PROCEDURE SchemaName.Testing_Passing_Parameter_in_SP ()
 LANGUAGE plpgsql 
 AS   
 $$ 
  
  DECLARE 
  FirstDayofWeek VARCHAR(50); 
  Unload_Query VARCHAR(65000); 

  BEGIN 

WITH CTE_FirstDayOfWeek AS (SELECT TO_CHAR(DATE_TRUNC('WEEK', GETDATE()), 'MMDDYYYY') AS FirstDayOfWeek) 
SELECT FirstDayOfWeek INTO FirstDayofWeek FROM CTE_FirstDayOfWeek; 

  Unload_Query := ' UNLOAD (''' || 'SELECT  *
                                   FROM SchemaName.TableName '
                                ||             
                           ''') 
                   TO  '''||'s3://BucketName/FolderName/Sample_File_'||FirstDayofWeek||'.csv'||''' 
                   CREDENTIALS ''' ||'aws_access_key_id=xxx'||';'||'aws_secret_access_key=xxx'||  '''
                   Header
                   ALLOWOVERWRITE
                   delimiter '''||','||'''
                   addquotes
                   PARALLEL OFF          
                          ';
  EXECUTE Unload_Query;

 END; 
 $$;

CALL SchemaName.Testing_Passing_Parameter_in_SP ();

Redshift 基于非常古老的 Postgres,其中不检测 PL/pgSQL 变量和 SQL 标识符之间的冲突。变量 FirstDayofWeek 与列 FirstDayofWeek 具有相同的名称。一般的解决方案是对 PL/pgSQL 变量使用前缀 _

DECLARE 
  _FirstDayofWeek VARCHAR(50); 
  _Unload_Query VARCHAR(65000);