Re: Updating a specific number of rows in pl/pgsql

From: "Peter Headland" <pheadland(at)actuate(dot)com>
To: "D'Arcy J(dot)M(dot) Cain" <darcy(at)druid(dot)net>
Cc: <pgsql-sql(at)postgresql(dot)org>
Subject: Re: Updating a specific number of rows in pl/pgsql
Date: 2009-08-11 16:55:36
Message-ID: 71F491F5DA99604A80DE49424BF3D02B0C7292D8@exchange8.actuate.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-sql

>Assuming that there is a unique identifier on queue

Alas, there is not. The PK is made up of 4 columns.

--
Peter Headland
Architect
Actuate Corporation

-----Original Message-----
From: D'Arcy J.M. Cain [mailto:darcy(at)druid(dot)net]
Sent: Tuesday, August 11, 2009 03:25
To: Peter Headland
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: [SQL] Updating a specific number of rows in pl/pgsql

On Mon, 10 Aug 2009 17:52:36 -0700
"Peter Headland" <pheadland(at)actuate(dot)com> wrote:
> I can get the rows I want to update like this:
>
> SELECT *
> FROM queue
> WHERE id = p_queue_id
> ORDER BY rank
> LIMIT p_number_of_items;
>
> Of course, there may not be p_number_of_items available in the queue.
>
> I want to update all the rows in the cursor in the same way:
>
> UPDATE queue SET assigned = TRUE;

Assuming that there is a unique identifier on queue, let's call it
queue_id, you should be able to do something like this:

UPDATE queue SET assigned = TRUE
WHERE queue_id IN (SELECT queue_id
FROM queue
WHERE id = p_queue_id
ORDER BY rank
LIMIT p_number_of_items);

--
D'Arcy J.M. Cain <darcy(at)druid(dot)net> | Democracy is three wolves
http://www.druid.net/darcy/ | and a sheep voting on
+1 416 425 1212 (DoD#0082) (eNTP) | what's for dinner.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Peter Headland 2009-08-11 17:05:28 Re: Updating a specific number of rows in pl/pgsql
Previous Message Merlin Moncure 2009-08-11 15:10:06 Re: keepalive problem with libpg connection

Browse pgsql-sql by date

  From Date Subject
Next Message Peter Headland 2009-08-11 17:05:28 Re: Updating a specific number of rows in pl/pgsql
Previous Message Rob Sargent 2009-08-11 15:40:51 Re: Determining logically unique entities across many partially complete rows where at least one column matches