在 COPY FROM 语句 postgresql 中使用变量



>我正在尝试将路径存储在变量中,以便在 Copy 语句中使用它,但是它不起作用。

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

最新更新