英文:
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;
$$;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论