Re: Separating data sets in a table

From: Oliver Elphick <olly(at)lfix(dot)co(dot)uk>
To: Andreas Tille <tillea(at)rki(dot)de>
Cc: PostgreSQL SQL <pgsql-sql(at)postgresql(dot)org>
Subject: Re: Separating data sets in a table
Date: 2002-08-26 08:36:48
Message-ID: 1030351008.20178.21.camel@linda
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Mon, 2002-08-26 at 07:26, Andreas Tille wrote:
> Well for sure this might be an option but as I said I receive the data
> in the dump format apropriate to use "COPY <tablemane> FROM <file>". Would
> you really like to suggest me to split those data sets into single lines?
> Moreover I'm not sure about how to catch the error messages of failed
> COPY statements.

How about this approach:

Create a temporary table (no constraints)

CREATE TEMP TABLE temptable AS
(SELECT * FROM tablename LIMIT 1);
DELETE FROM temptable;

Copy all data into the temporary table

COPY temptable FROM 'filepath';

Select from the temporary table all items that satisfy the
constraints, insert them into the real table and delete them from
the temporary table:

BEGIN;
INSERT INTO tablename (SELECT * FROM temptable WHERE ...);
DELETE FROM temptable WHERE ...;
COMMIT;

All good data should now be in place. The temporary table should
now contain only those items that do not satisfy the constraints for
the real table.

--
Oliver Elphick Oliver(dot)Elphick(at)lfix(dot)co(dot)uk
Isle of Wight, UK
http://www.lfix.co.uk/oliver
GPG: 1024D/3E1D0C1C: CA12 09E0 E8D5 8870 5839 932A 614D 4C34 3E1D 0C1C
========================================
"Seeing then that all these things shall be dissolved,
what manner of persons ought ye to be? You ought to
live holy and godly lives as you look forward to the
day of God and speed its coming."
II Peter 3:11,12

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Gaetano Mendola 2002-08-26 09:29:37 unsubscribe
Previous Message David Wheeler 2002-08-26 07:05:20 ANNOUNCE: Bricolage 1.3.3