Re: COPY - Ignore Problems

From: Oliver Elphick <olly(at)lfix(dot)co(dot)uk>
To: Ron St-Pierre <rstpierre(at)syscor(dot)com>
Cc: PostgreSQL general list <pgsql-general(at)postgresql(dot)org>
Subject: Re: COPY - Ignore Problems
Date: 2003-12-23 11:51:15
Message-ID: 1072180274.18218.553.camel@linda.lfix.co.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Mon, 2003-12-22 at 23:47, Ron St-Pierre wrote:
> I wish to insert data into a table from a very large text file (from a
> cron script) using COPY. However if the lName (TEXT), fName(TEXT),
> workDate(DATE) already exist I don't want to insert data and just want
> to move onto the next record. Is there any way I can tell my bash
> script/COPY to ignore the case where the unique constraint exists
> (lName,fName,workDate), and move on to the next record?

CREATE TEMPORARY TABLE mytemp (
LIKE real_table, PRIMARY KEY (lname, fname, workdate)
);
COPY mytemp FROM '/my/input/file';
INSERT INTO real_table (
SELECT * FROM mytemp AS t
WHERE NOT EXISTS (
SELECT * FROM real_table AS r
WHERE r.lname = t.lname AND
r.fname = t.fname AND
r.workdate = f.workdate
)
);

NB: your mixed case names are automatically converted to lower case
unless you quote them.
--
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
========================================
"And this shall be a sign unto you; Ye shall find the
babe wrapped in swaddling clothes, lying in a manger."
Luke 2:12

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Roderick A. Anderson 2003-12-23 13:08:39 Re: CHECK versus a Table for an enumeration
Previous Message Michael Meskes 2003-12-23 09:59:14 Re: ecpg question