title: How do you pass a parameter to a pgsql command within a function?
body: <p>I use the copy function in postgresql often and I thought I'd create a function to make use of it.
  So let's say i have the following table</p>
  
  <pre><code>create table users
  (
     userid serial primary key,
     prefix varchar(10) not null,
     firstname varchar(255) not null,
     lastname varchar(255) not null,
     email varchar(500) not null,
     password varchar(255) not null,
     created timestamp not null,
     modified timestamp not null
  );
  </code></pre>
  
  <p>and the following function</p>
  
  <pre><code>create function getUsersCSV(  )
  RETURNS void AS
  $BODY$
    BEGIN
      COPY (select * from users) 
      TO 'c:\temp\users.csv' 
      WITH HEADER 
      CSV QUOTE '"' DELIMITER ',';
    END
  $BODY$
  LANGUAGE PLPGSQL;
  </code></pre>
  
  <p>function works but if I change the function to </p>
  
  <pre><code>create function getUsersCSV( filepath varchar )
  RETURNS void AS
  $BODY$
    BEGIN
      COPY (select * from users) 
      TO filepath 
      WITH HEADER 
      CSV QUOTE '"' DELIMITER ',';
    END
  $BODY$
  LANGUAGE PLPGSQL;
  </code></pre>
  
  <p>it throws the following syntax error
  ERROR: syntax error at or near "filepath"
  SQL state: 42601</p>
  
  <p>Is there anyway to parameterize the destination value of the copy command?</p>