result of UPDATE ... RETURNING not usable / "real" SELECT required?

From: Daniel Frey <d(dot)frey(at)gmx(dot)de>
To: pgsql-general(at)postgresql(dot)org
Subject: result of UPDATE ... RETURNING not usable / "real" SELECT required?
Date: 2008-08-05 19:12:05
Message-ID: 1217963525.25114.8.camel@localhost
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi,

here's a short example, the problem is at the end:

DROP TABLE IF EXISTS foo;
CREATE TABLE foo ( id SERIAL PRIMARY KEY, c2 VARCHAR NOT NULL );

INSERT INTO foo VALUES ( DEFAULT, 'foo' );
INSERT INTO foo VALUES ( DEFAULT, 'bar' );
INSERT INTO foo VALUES ( DEFAULT, 'baz' );

DROP TABLE IF EXISTS bar;
CREATE TABLE bar ( id SERIAL PRIMARY KEY, c2 VARCHAR NOT NULL );

INSERT INTO bar VALUES ( DEFAULT, 'bla' );
INSERT INTO bar VALUES ( 4, 'blubb' );

SELECT * FROM foo;
SELECT * FROM bar;

-- This works
BEGIN;
UPDATE foo SET c2=bar.c2 FROM bar WHERE foo.id=bar.id RETURNING bar.id;
INSERT INTO foo (
SELECT * FROM bar WHERE id NOT IN (
SELECT id FROM foo
)
);
COMMIT;

-- This doesn't:
INSERT INTO foo (
SELECT * FROM bar WHERE id NOT IN (
UPDATE foo SET c2=bar.c2 FROM bar WHERE foo.id=bar.id RETURNING
bar.id
)
);

Question: Why is the last command not accepted? (ERROR: syntax error at
or near "foo" (the "foo" after UPATE)) Shouldn't the result structure of
"UPDATE ... RETURNING ..." be the same as the result structure from
"SELECT id FROM foo" (note: structure, not content in the example
above)?

Regards, Daniel

Browse pgsql-general by date

  From Date Subject
Next Message SHARMILA JOTHIRAJAH 2008-08-05 19:12:28 Heikkki's Visibility Map patch for postgres 8.4 ?
Previous Message Scott Marlowe 2008-08-05 19:02:10 Re: Moved database question