Landister
Landister

Reputation: 2224

Is there a way to set a variable in a PostgreSQL statment that can be used in a file path name

I.E.

In the following statment

COPY "TableName"
FROM 'MyVARIABLE/filename.csv';

Can I set my variable with something?

Upvotes: 1

Views: 1600

Answers (1)

filiprem
filiprem

Reputation: 7144

Yes, but only with procedural language not plain sql.

create function copyin( _tablename name, _filename text )
returns void
language plpgsql as $this$
declare sql text;
begin
  sql := 'COPY ' || quote_ident(_tablename) || ' FROM ' || quote_literal(_filename);
  execute sql;
  return;
end;
$this$;

SELECT copyin( 'froobles', '/tmp/froobles.csv' );

Upvotes: 1

Related Questions