Re: sql statement how to do ?

From: Manfred Koizar <mkoi-pg(at)aon(dot)at>
To: pgsql-sql(at)postgresql(dot)org
Subject: Re: sql statement how to do ?
Date: 2002-07-05 12:40:10
Message-ID: v34biu447apt49qoru6ib7r1quifes4526@4ax.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Fri, 5 Jul 2002 09:03:38 +0000 (UTC), juerg(dot)rietmann(at)pup(dot)ch wrote:
>INSERT INTO auftrag (SELECT * FROM auftrag where a_id = '12345');
>
>The problem is, that the table auftrag has a primay key called pk_auftrag.
>Do this I get an error regarding duplicate pk_auftrag. Is there a way to
>spare pk_auftrag somehow ?

Juerg, is a_id your primary key? My examples are based on this
assumption, but if it is not, you still get the point, which is:
You can't use SELECT * here.

If you know the new a_id in advance:

INSERT INTO auftrag (a_id, col2, col3, ...)
SELECT '67890', col2, col3, ...
FROM auftrag
WHERE a_id = '12345';

If a_id is a serial or in any other way supplied automatically by a
DEFAULT clause or a trigger:

INSERT INTO auftrag (col2, col3, ...)
SELECT col2, col3, ...
FROM auftrag
WHERE a_id = '12345';

HTH.
Servus
Manfred

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Mark Frazer 2002-07-05 13:38:30 Re: Possible Bug regarding temp tables (sql or psql?)
Previous Message Achilleus Mantzios 2002-07-05 12:37:22 Re: how to use nextval()