pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Herouth Maoz <herouth@oumail.openu.ac.il>
To: Leslie Mikesell <les@Mcs.Net>
Cc: pgsql-sql@postgreSQL.org
Subject: Re: [SQL] idiom to move records?
Date: Mon, 26 Oct 1998 11:34:41 +0200
Message-ID: <l03110701b259f11ba64d@[147.233.159.109]> (raw)
In-Reply-To: <199810260531.XAA14075@Mars.mcs.net>

At 7:31 +0200 on 26/10/98, Leslie Mikesell wrote:


> Is there a handy way to move a set of records to a different
> table in sql?  I'd like to do this based on a WHERE clause
> and have it work atomically such that it will not lose new
> records added between the step that copies to the other table
> and deleting the copied records.

Do it in a transaction, then. Assuming that the target table is defined
(not a temporary table you create ad-hoc), you do:

BEGIN TRANSACTION;

LOCK TABLE source_table;

INSERT INTO target_table (col1, col2, col3)
SELECT (expr1, expr2, expr3)
FROM source_table
WHERE <where condition here>;

DELETE FROM source_table
WHERE <where condition here>;

COMMIT;



Herouth

--
Herouth Maoz, Internet developer.
Open University of Israel - Telem project
http://telem.openu.ac.il/~herutma





view thread (3+ messages)  latest in thread

Message-ID: <l03110701b259f11ba64d@[147.233.159.109]>
Permalink:  ../l03110701b259f11ba64d@%5B147.233.159.109%5D/
Also on:    postgresql.org/message-id/l03110701b259f11ba64d@[147.233.159.109]

 · 

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: herouth@oumail.openu.ac.il, les@Mcs.Net
  Subject: Re: [SQL] idiom to move records?
  In-Reply-To: <l03110701b259f11ba64d@[147.233.159.109]>
  References: <199810260531.XAA14075@Mars.mcs.net> <l03110701b259f11ba64d@[147.233.159.109]>

* 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