Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I've searched around for an answer and it seems definitive but I figured I would double check with the Stack Overflow community:
Here's what I'm trying to do:

INSERT INTO my_table VALUES (a, b, c)
RETURNING (SELECT x, y, z FROM x_table, y_table, z_table
WHERE xid = a AND yid = b AND zid = c)

I get an error telling me I can't return more than one column.
It works if I tell it SELECT x FROM x_table WHERE xid = a.

Is this at all possible in a single query as opposed to creating a seperate SELECT query?

I'm using PostgreSQL 8.3.

share|improve this question

2 Answers 2

Try this.

with aaa as (
    INSERT INTO my_table VALUES(a, b, c)
    RETURNING a, b, c)
SELECT x, y, z FROM x_table, y_table, z_table
WHERE xid = (select a from aaa)
  AND yid = (select b from aaa)
  AND zid = (select c from aaa);

In 9.3 similar query works.

share|improve this answer
    
No good. Like I mentioned, using 8.3. –  itchy23 Dec 11 '13 at 1:23
up vote 1 down vote accepted

@corvinusz answer was wrong for 8.3 but gave me a great idea that worked so thanks!

INSERT INTO my_table VALUES (a, b, c)
RETURNING (SELECT x FROM x_table WHERE xid = a),
  (SELECT y FROM y_table WHERE yid = b),
  (SELECT z FROM z_table WHERE zid = c)

I have no idea why the way it's stated in the question is invalid but at least this works.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.