1

I am trying to store a path in a variable, to use it in a Copy statement, but it is not working.

DO $$ 

DECLARE PATH char(100):='/home/gabriela/Documents/q_types.csv';

BEGIN

  CREATE TABLE mydbschema.example(

    ID integer NOT NULL PRIMARY KEY,

    Value char(15) NOT NULL 
    );

  COPY mydbschema.example FROM 'PATH' DELIMITER ',';

END $$;
1
  • COPY mydbschema.example FROM PATH DELIMITER ','; didnt work either Commented Jan 11, 2018 at 2:15

1 Answer 1

1
create or replace function load(file_name text)
returns void as $$
Declare
 csv_path text:= '/home/gabriela/Documents/';
 t_path text:=csv_path||file_name;

begin
    -- copy the data from csv file
    execute format('copy example from %L with delimiter '','' quote ''}'' csv', t_path);
end;

$$ language plpgsql;

DO $$ BEGIN
    PERFORM load('example.csv');
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.