如何使用COPY命令在PostgreSQL过程与外部变量?

k2arahey  于 2023-06-05  发布在  PostgreSQL
关注(0)|答案(1)|浏览(246)

在PostgreSQL中使用COPY创建过程,FROM关键字既不识别外部变量也不识别内部变量。

CREATE OR REPLACE PROCEDURE YYY (
    external_variable varchar
)
language plpgsql
AS $$
declare 
internal_variable varchar;
begin
COPY TEMP_ALL_ROWS_BKMVDATA FROM external_variable DELIMITER ',' CSV HEADER;

end;
$$

回复:

ERROR:  syntax error at or near "external_variable"
LINE 9: COPY TEMP_ALL_ROWS_BKMVDATA FROM external_variable DELIMITER...
                                         ^
SQL state: 42601
Character: 162

与internal_variable相同。
我已经试着忽略变量部分,以确保它周围的一切工作,确实。

CREATE OR REPLACE PROCEDURE YYY (
    external_variable varchar
)
language plpgsql
AS $$
declare 
internal_variable varchar;
begin
COPY TEMP_ALL_ROWS_BKMVDATA FROM '' DELIMITER ',' CSV HEADER;

end;
$$
CREATE PROCEDURE

Query returned successfully in 322 msec.
sy5wg1nm

sy5wg1nm1#

复制命令在创建或替换中不起作用,请使用执行命令

CREATE OR REPLACE PROCEDURE YYY (
    external_variable varchar
)
LANGUAGE plpgsql
AS $$
BEGIN
    EXECUTE 'COPY TEMP_ALL_ROWS_BKMVDATA FROM ''' || external_variable || ''' DELIMITER '','' CSV HEADER;';
END;
$$;

相关问题