Re: Switching from MySQL: ON DUPLICATE KEY UPDATE, plpgsql function - Mailing list pgsql-general

From Chris Spotts
Subject Re: Switching from MySQL: ON DUPLICATE KEY UPDATE, plpgsql function
Date
Msg-id 002f01c9fa3f$9e54efe0$dafecfa0$@com
Whole thread Raw
In response to Re: Switching from MySQL: ON DUPLICATE KEY UPDATE, plpgsql function  (APseudoUtopia <apseudoutopia@gmail.com>)
List pgsql-general
> >
> > Wouldn't you just be looking for something like:
> >
> > BEGIN;
> >  EXECUTE 'insert into forums_readposts values ('...')';
> >        EXCEPTION when unique_violation THEN
> >                EXECUTE 'update forums_readposts set lastpostread =
> '...' ';
> > END;

> > The logic as i read your post is. If the user's never done a
> "catchup" operation
> > before, this will create the record. If he has, then it will update
> this record
> > to reflect the new transid.
> >
> > Dave
> >
>
> Hmm, yeah. I'm new to PostgreSQL, and have little experience with
> MySQL other than basic queries and joins. I didn't get to the part in
> the docs that covers EXECUTE yet, haha. That looks like it would do
> exactly what I want it to.
[Spotts, Christopher]
There's a good example and some documentation of what Dave said in the manual at the bottom of this page.
http://www.postgresql.org/docs/current/static/plpgsql-control-structures.html
You should really only need to "EXECUTE" here if you have some dynamically determined columns or tables.  The "magic"
hereis a combination of "IF found" and "EXCEPTION WHEN unique violation" 




pgsql-general by date:

Previous
From: Henry
Date:
Subject: Re: Regex Character-Class
Next
From: Tony Webb
Date:
Subject: Big Delete Consistently Causes a Crash