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

huangapple go评论74阅读模式
英文:

How to use COPY command in PostgreSQL procedure with an external variable?

问题

Creating a procedure with COPY in PostgreSQL, the FROM keyword does not recognize neither external nor internal variable.

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;
$$

response:

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

Same for internal_variable.

I've tried neglecting the variable part to make sure everything around it works and indeed.

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.
英文:

Creating a procedure with COPY in PostgreSQL, the FROM keyword does not recognize neither external nor internal variable.

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;
$$

response:

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

Same for internal_variable.

I've tried neglecting the variable part to make sure everything around it works and indeed.

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.

答案1

得分: 2

在创建或替换中,复制命令不起作用,请改用执行。
英文:

Copy command wont work in create or replace , Use execute instead

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;
$$;

huangapple
  • 本文由 发表于 2023年5月29日 17:40:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76356232.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定