Re: [GENERAL] Easy form of insert if it isn't already there?

2012-02-19 Thread Jasen Betts
On 2012-02-15, Chris Angelico ros...@gmail.com wrote: Periodically I find myself wanting to insert into some table, specifying the primary key column(s), but to simply ignore the request if it's already there. Currently I have two options: 1) Do the insert as normal, but suppress errors.

Re: [GENERAL] Easy form of insert if it isn't already there?

2012-02-15 Thread Philip Couling
This must be a function or trigger to break one statement into two. You could of course simply use two separate statements in PHP as long as they are in the same transaction. If you're going to perform this action in two steps then putting both in a function or trigger is often preferable.

Re: [GENERAL] Easy form of insert if it isn't already there?

2012-02-15 Thread Berend Tober
Chris Angelico wrote: On Wed, Feb 15, 2012 at 5:26 PM, Bartosz Dmytrakbdmyt...@eranet.pl wrote: e.g. You can use BEGIN... EXCEPTION END, good example of such approach is there: http://www.postgresql.org/docs/9.1/static/plpgsql-control-structures.html#PLPGSQL-UPSERT-EXAMPLE; I wonder

Re: [GENERAL] Easy form of insert if it isn't already there?

2012-02-15 Thread Bartosz Dmytrak
Maybe to show how found works and how to ignore errors - that is my assumption only. Regards, Bartek 2012/2/15 Berend Tober bto...@broadstripe.net Chris Angelico wrote: On Wed, Feb 15, 2012 at 5:26 PM, Bartosz Dmytrakbdmyt...@eranet.pl wrote: e.g. You can use BEGIN... EXCEPTION

[GENERAL] Easy form of insert if it isn't already there?

2012-02-14 Thread Chris Angelico
Periodically I find myself wanting to insert into some table, specifying the primary key column(s), but to simply ignore the request if it's already there. Currently I have two options: 1) Do the insert as normal, but suppress errors. SAVEPOINT foo; INSERT INTO table (col1,col2,col3) VALUES

Re: [GENERAL] Easy form of insert if it isn't already there?

2012-02-14 Thread Bartosz Dmytrak
Hi, similar topic is in NOVICE mailing list: http://archives.postgresql.org/pgsql-novice/2012-02/msg00034.php e.g. You can use BEGIN... EXCEPTION END, good example of such approach is there: http://www.postgresql.org/docs/9.1/static/plpgsql-control-structures.html#PLPGSQL-UPSERT-EXAMPLE ;

Re: [GENERAL] Easy form of insert if it isn't already there?

2012-02-14 Thread Chris Angelico
On Wed, Feb 15, 2012 at 5:26 PM, Bartosz Dmytrak bdmyt...@eranet.pl wrote: Hi, similar topic is in NOVICE mailing list: http://archives.postgresql.org/pgsql-novice/2012-02/msg00034.php e.g. You can use BEGIN... EXCEPTION END, good example of such approach is there: 

Re: [GENERAL] Easy form of insert if it isn't already there?

2012-02-14 Thread Bartosz Dmytrak
Yes it is. You can implement trigger on table to check if inserted record is new. Still it is on DB side. I don't know PHP well enough but I think You can call function e.g. SELECT myschema.InsertWhenNew (val1, val2, val3); in the same way as You call INSERTS Regards, Bartek 2012/2/15 Chris