Re: Trigger/copy issue

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Sean Davis <sdavis2(at)mail(dot)nih(dot)gov>
Cc: pgsqlnovice <pgsql-novice(at)postgresql(dot)org>
Subject: Re: Trigger/copy issue
Date: 2005-02-04 18:26:39
Message-ID: 15974.1107541599@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

Sean Davis <sdavis2(at)mail(dot)nih(dot)gov> writes:
> I have the following setup and data file that I want to load using copy
> (8.0.0, macos). The trigger function is to "clean" the input data (see
> earlier posts on the subject). However, it seems as if the trigger
> function is not doing what I would have thought. I want it to replace
> '-' in the data in certain columns with NULLs.

> CREATE TABLE g_refseq (
> ...
> chrom_gi integer,
> ...

> if (NEW.chrom_gi='-') THEN
> NEW.chrom_gi:= 'NULL';
> END IF;

This isn't gonna work, because '-' is not a legal value of an integer
column and so the data conversion would have failed long before your
trigger gets to execute.

(You are also wrong in using quotes around the keyword NULL, but that's
a secondary problem.)

If you have to import data that's defined like this, I'd suggest loading
into a temporary table that's declared as all unconstrained text
columns, and then converting with something like

INSERT INTO realtable
SELECT
...
(case when chrom_gi = '-' then null else chrom_gi::integer),
...
FROM temptable;

regards, tom lane

In response to

Browse pgsql-novice by date

  From Date Subject
Next Message William Yu 2005-02-04 19:27:58 Re: Temp table exists test??
Previous Message Sean Davis 2005-02-04 17:25:13 Trigger/copy issue