pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Magnus Hagander <mha@sollentuna.net>
To: Owen Jacobson <ojacobson@osl.com>
To: postgreSQL (SQL) <pgsql-sql@postgresql.org>
Subject: Re: Multi-row update w. plpgsql function
Date: Wed, 14 Dec 2005 10:15:31 +0100
Message-ID: <6BCB9D8A16AC4241919521715F4D8BCE6C7D64@algol.sollentuna.se> (raw)

> > Imagine a table called 'message_table':
> > 
> > mid | message | status
> > ----+---------+-------
> >   1  |  Text1   |  H
> >   2  |  Text2   |  H
> >   3  |  Text3   |  H
> >   4  |  Text4   |  H
> >  
> > A web page presents the user with all messages flagged with 
> 'H'. User 
> > checks messages 1,3 and 4 and submits form.
> > (i.e. approved=1&approved=3&approved=4)
> >  
> > After performing postgreSQL update, rows 1, 3 and 4 would 
> be updated 
> > to:
> >  
> > mid | message | status
> > ----+---------+-------
> >   1  |  Text1   |  A
> >   2  |  Text2   |  H
> >   3  |  Text3   |  A
> >   4  |  Text4   |  A
> 
> BEGIN;
> UPDATE message_table SET status = 'A' WHERE mid = 1; UPDATE 
> message_table SET status = 'A' WHERE mid = 3; UPDATE 
> message_table SET status = 'A' WHERE mid = 4; COMMIT;
> 
> would do that.  Have your application generate an appropriate 
> UPDATE line for each "approved" entry in the form data, wrap 
> it in a transaction, and away you go.

It would probably be even more efficient to do:
UPDATE message_table SET status = 'A' WHERE mid IN (1,3,4)

and then use client code to generate the comma-separated list of ids.
(Don't forget to make sure they are actual integers so you don't get a
sql injection from it - I don't think parametrised queries can deal with
comma lists)

//Magnus



view thread (6+ messages)  latest in thread

Message-ID: <6BCB9D8A16AC4241919521715F4D8BCE6C7D64@algol.sollentuna.se>
Permalink:  ../6BCB9D8A16AC4241919521715F4D8BCE6C7D64@algol.sollentuna.se/
Also on:    postgresql.org/message-id/6BCB9D8A16AC4241919521715F4D8BCE6C7D64@algol.sollentuna.se

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: mha@sollentuna.net, ojacobson@osl.com
  Subject: Re: Multi-row update w. plpgsql function
  In-Reply-To: <6BCB9D8A16AC4241919521715F4D8BCE6C7D64@algol.sollentuna.se>
  References: <6BCB9D8A16AC4241919521715F4D8BCE6C7D64@algol.sollentuna.se>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox