【问题标题】:only one AS items needed for language "plpgsql";语言“plpgsql”只需要一个 AS 项;
【发布时间】:2021-03-26 05:40:52
【问题描述】:

我正在尝试在 stored procedure 中运行 copy 命令。 此复制命令从aws s3 复制到aws redshift 中的表

这是复制命令

copy schema1_ghsheet.ghseet_temp
from
''''s3://root2/rawfiles/''''
iam_role ''''arn:aws:iam::743:role/redshift''''
csv DELIMITER ',' IGNOREHEADER 1 TRUNCATECOLUMNS;

我正在尝试将其添加到存储过程中,现在看起来像这样。

我在这里尝试创建一个temp 表,其中包含来自s3 的所有数据,该表正在使用copy 命令复制到temp 表中。


CREATE OR REPLACE PROCEDURE proc_test() LANGUAGE plpgsql
AS
'
BEGIN
drop table if exists 
schema1_ghsheet.ghseet_temp
;
create table 
schema1_ghsheet.ghseet_temp(emp_id int, emp_name varchar(100),hrly_rate int,mod_timestamp timestamp)
;

copy schema1_ghsheet.ghseet_temp
from
''''s3://root2/rawfiles/''''
iam_role ''''arn:aws:iam::743:role/redshift''''
csv DELIMITER ',' IGNOREHEADER 1 TRUNCATECOLUMNS;
drop table if exists 
schema1_ghsheet.ghseet_main
;

create table 
schema1_ghsheet.ghseet_main
as 
select h1.emp_id,h1.emp_name,h1.hrly_rate,h1.mod_timestamp
from schema1_ghsheet.ghseet_hstry h1
inner join (
select emp_id ,emp_name , max(mod_timestamp ) mod_timestamp
from schema1_ghsheet.ghseet_hstry
group by 1,2
) h2
on h1.emp_id=h2.emp_id
and h1.mod_timestamp=h2.mod_timestamp
group by 1,2,3,4
;

END;

'

但这会引发错误:

only one AS items needed for language "plpgsql";

那么,如何在stored procedure 中添加copy 命令还是需要单独调用copy 命令?

【问题讨论】:

    标签: sql amazon-web-services stored-procedures amazon-redshift


    【解决方案1】:

    COPY is an allowed DML statement within a stored procedure.

    看起来错误是指 AS 关键字。据我所知,documentation 中的语法要求将 AS 之后的脚本包装在 $$ 中,并且 LANGUAGE plpgsql 指定应该出现在 script body 之后。文档中给出的示例是:

    CREATE OR REPLACE PROCEDURE test()
    AS $$
    BEGIN
      SELECT 1 a;
    END;
    $$
    LANGUAGE plpgsql
    ;
    /
    

    所以你应该像下面这样改变脚本的顺序(我现在不能在本地测试这个)

    CREATE OR REPLACE PROCEDURE proc_test()
    AS
    $$
    BEGIN
    drop table if exists 
    schema1_ghsheet.ghseet_temp
    ;
    create table 
    schema1_ghsheet.ghseet_temp(emp_id int, emp_name varchar(100),hrly_rate int,mod_timestamp timestamp)
    ;
    
    copy schema1_ghsheet.ghseet_temp
    from
    ''''s3://root2/rawfiles/''''
    iam_role ''''arn:aws:iam::743:role/redshift''''
    csv DELIMITER ',' IGNOREHEADER 1 TRUNCATECOLUMNS;
    drop table if exists 
    schema1_ghsheet.ghseet_main
    ;
    
    create table 
    schema1_ghsheet.ghseet_main
    as 
    select h1.emp_id,h1.emp_name,h1.hrly_rate,h1.mod_timestamp
    from schema1_ghsheet.ghseet_hstry h1
    inner join (
    select emp_id ,emp_name , max(mod_timestamp ) mod_timestamp
    from schema1_ghsheet.ghseet_hstry
    group by 1,2
    ) h2
    on h1.emp_id=h2.emp_id
    and h1.mod_timestamp=h2.mod_timestamp
    group by 1,2,3,4
    ;
    
    END;
    
    $$ 
    LANGUAGE plpgsql;
    

    【讨论】:

      猜你喜欢
      • 2010-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-07
      • 1970-01-01
      • 1970-01-01
      • 2013-02-13
      • 1970-01-01
      相关资源
      最近更新 更多