Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><a href="http://www.postgresql.org/docs/current/interactive/sql-copy.html" rel="nofollow">COPY</a> is a bit odd as it sort of treats its <code>query</code> argument as a string even though it isn't written as a string. The result is that the <code>query</code>:</p> <pre><code>SELECT * FROM retrieve_info($1, $2) </code></pre> <p>isn't executed in the context of the function, it is executed in the context of COPY itself. Even though you say:</p> <pre><code>copy (select * from t) ... </code></pre> <p>it is treated more as though you wrote:</p> <pre><code>copy 'select * from t' ... </code></pre> <p>so by the time the query is executed, the function parameters no longer have any meaning, the <code>query</code> argument to COPY may look like it would behave like a closure in other languages but it doesn't, it acts more like a string that gets passed to <code>eval</code>.</p> <p>You can get around this strangeness by using the usual Kludge of Last Resort: <a href="http://www.postgresql.org/docs/current/interactive/plpgsql-statements.html#PLPGSQL-STATEMENTS-EXECUTING-DYN" rel="nofollow">dynamic SQL</a>. You should get better results if you write your function to use string wrangling and EXECUTE:</p> <pre><code>create or replace function print_out(text, text) returns void as $$ begin execute 'copy (' || 'select * from retrieve_info' || '(' || quote_literal($1) || ',' || quote_literal($2) || ')' || ') to ''myfilepath/test.csv'' with csv header;'; end; $$ language plpgsql; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload