Skip site navigation (1) Skip section navigation (2)

Peripheral Links

Header And Logo

PostgreSQL
| The world's most advanced open source database.

Site Navigation

Search archives
  Advanced Search

Re: Errors attempting to insert duplicate values


  • From: Ed Loehr <eloehr(at)austin(dot)rr(dot)com>
  • To: Bryan White <bryan(at)arcamax(dot)com>
  • Cc: pgsql-general <pgsql-general(at)postgresql(dot)org>
  • Subject: Re: Errors attempting to insert duplicate values
  • Date: Fri, 09 Jun 2000 09:46:46 -0500
  • Message-id: <39410356.28E029E@austin.rr.com> <text/plain>

Bryan White wrote:
> 
> I need to insert a record only if it does not
> exist.  If it does exists then I do nothing.  However I do need to know if
> it was inserted.  Its seems most efficient to just attempt the insert and
> look at the result.  My only complaint is the log file is littered the error
> messages about attempting to insert a duplicate.

How about something like this?

	INSERT INTO foo (id, ...)
		SELECT 14, ... <=== literal insert values
		FROM foo
		WHERE NOT EXISTS (SELECT * FROM foo WHERE id = 14)

It'll insert 1 or 0 records, and it won't try to insert a duplicate...

Regards,
Ed Loehr



Home | Main Index | Thread Index

Privacy Policy | PostgreSQL Archives hosted by Command Prompt, Inc. | Designed by tinysofa
Copyright © 1996 – 2008 PostgreSQL Global Development Group