1

I have a problem with my COPY on sqlWorkbench. I need to do a copy function from db TO a path that I create at the moment because is different for each table. I have this error ' ERROR: syntax error at or near "||"'

I have created the path with the concat operator but i have the error at ||

CREATE OR REPLACE FUNCTION copy_func(IN table_name text, IN days numeric) 
RETURNS void AS 
$body$ 
DECLARE    

BEGIN        

 COPY (SELECT * FROM table_name WHERE backup = 1) TO '/var/audiobays/logs/audit/' || table_name || '_deletions_(' || date-days|| ').csv' CSV DELIMITER ',' HEADER; 
END 
$body$ 
LANGUAGE plpgsql;

1 Answer 1

2

COPY can only take string literals for the path. If you need a dynamic path you can use dynamic SQL to build and execute the statement.

DECLARE    
  statement text;
BEGIN        
  statement := 'COPY (SELECT * FROM table_name WHERE backup = 1) TO ''/var/audiobays/logs/audit/' || table_name || '_deletions_(' || date-days|| ').csv'' CSV DELIMITER '','' HEADER;';
  EXCUTE statement;  
END;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.