0

In a WITH RECURSIVE query, is it possible to to use COPY TO in the same way INSERT INTO?

I'm trying to write a file from a recursive query without having to save it to a table first.

1
  • Please Edit your question and add some sample data and the expected output based on that data. Formatted text please, no screen shots. edit your question - do not post code or additional information in comments. Commented Oct 20, 2017 at 15:35

1 Answer 1

3

sure why not, eg:

t=# copy ( WITH RECURSIVE t(n) AS (
    VALUES (1)
  UNION ALL
    SELECT n+1 FROM t WHERE n < 100
)
SELECT sum(n) FROM t) to '/tmp/rc';
COPY 1
Time: 5.161 ms
t=# \! cat /tmp/rc
5050
Sign up to request clarification or add additional context in comments.

2 Comments

I think the OP might be trying to use the COPY inside the CTE. But if not, this is a decent attempt to answer a very vague question.
hm... in that case it would take dynamic plpgsql I guess... but then why CTE?.. Well - lets see what OP says :) good point though - I reread the OP - yes it definetely might be the request

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.