Received: from makus.postgresql.org (makus.postgresql.org [98.129.198.125]) by mail.postgresql.org (Postfix) with ESMTP id 32F081B505ED for ; Wed, 14 Mar 2012 09:30:41 -0300 (ADT) Received: from nm14-vm0.bullet.mail.sp2.yahoo.com ([98.139.91.246]) by makus.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1S7nLi-0007jK-LE for pgsql-sql@postgresql.org; Wed, 14 Mar 2012 12:30:40 +0000 Received: from [98.139.91.69] by nm14.bullet.mail.sp2.yahoo.com with NNFMP; 14 Mar 2012 12:30:25 -0000 Received: from [98.139.91.29] by tm9.bullet.mail.sp2.yahoo.com with NNFMP; 14 Mar 2012 12:30:25 -0000 Received: from [127.0.0.1] by omp1029.mail.sp2.yahoo.com with NNFMP; 14 Mar 2012 12:30:25 -0000 X-Yahoo-Newman-Id: 857254.64205.bm@omp1029.mail.sp2.yahoo.com Received: (qmail 10326 invoked from network); 14 Mar 2012 12:30:25 -0000 DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.com; h=DKIM-Signature:X-Yahoo-Newman-Property:X-YMail-OSG:X-Yahoo-SMTP:Received:References:In-Reply-To:Mime-Version:Content-Transfer-Encoding:Content-Type:Message-Id:Cc:X-Mailer:From:Subject:Date:To; b=G4UroI0O2fUTa1A7m0xSXducTCVhaWiWq/+/Pu9VDNikRwy1kO31VRopTArVRlXlJ3UeUIDJpFNQd1sIX3XE1blUGOABrI5ITqQN4HwyTDRBveM3o6LQFC0et7nnNjkTi0ANeOEdT7cemBbXAaGo6W2ugWkMvvDLph91QfTJ9js= ; DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s1024; t=1331728225; bh=FUUggmTff43FEM34E7e1lo2cDj4tu1VFJ2Nf9Q1WQvE=; h=X-Yahoo-Newman-Property:X-YMail-OSG:X-Yahoo-SMTP:Received:References:In-Reply-To:Mime-Version:Content-Transfer-Encoding:Content-Type:Message-Id:Cc:X-Mailer:From:Subject:Date:To; b=wPzOz9oc5Fp9E6vz5c97oShXXD2xA6WjBLSXmT2TpbY9OmxO2MqVvk3nuihQCm90BRaN7gmwmaa5WR31iZOi3FdjZHYCJpsSiE0NNKwNOl14mYHqtFmJnoYTO23GrxRGpZPlDtMRjROMQSaK/ayiIEfvPrICq2Lp+4fKQHLviCU= X-Yahoo-Newman-Property: ymail-3 X-YMail-OSG: h3.7fSMVM1mQnV7uwaXZpOv7lsYMRb48lEPF_5H4rSP0oYh gW7SFOXj18YGcte5AzRNS4TItrJ1_Xooj7grU2HD7kbKFH6KKnVwFVxid8U7 nHcV.Y7ffPc3B9_uZQG1SNXQQduw1APsgreG72JEyhGCy7RKS1mIA2LLtyQa cm8gg3LNQcxukauMlLK1uSEU8MNRHchjvVrkV6flMoRV1cNRPpF_S3U06X4i BN_l.tZDx_sJbyuZgWN4tXuqjy9Udo9BECROgkCfHJbq6aQv7h3Chqlbjf1W uMUtj6XMPUBbTa7aLvsaLUkhpnGJShzOUFXdtP8BxFawQYsLp1fDGobukaYB g_BqvwRZqCKr2PEKHbEo1UN_9bqMydsMGLEaQB4KMxmxsS6B.H.fCHuLw2O8 3NYvdLmOtjkLWE1a7umyC X-Yahoo-SMTP: mpGJl6eswBD2IBufoVEg0Pa8gg-- Received: from [192.12.17.102] (polobo@24.93.23.188 with xymcookie) by smtp111-mob.biz.mail.gq1.yahoo.com with SMTP; 14 Mar 2012 05:30:25 -0700 PDT References: <4F5F91F6.2020103@turnkey.bz> In-Reply-To: <4F5F91F6.2020103@turnkey.bz> Mime-Version: 1.0 (1.0) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=us-ascii Message-Id: <6C709AC8-3C03-4E4D-B978-3939FB49591F@yahoo.com> Cc: "PostgreSQL \(SQL\)" X-Mailer: iPad Mail (9A405) From: David Johnston Subject: Re: help on a function with exception Date: Wed, 14 Mar 2012 08:30:25 -0400 To: "M. D." X-Pg-Spam-Score: -1.9 (-) X-Archive-Number: 201203/44 X-Sequence-Number: 36439 On Mar 13, 2012, at 14:29, "M. D." wrote: > Hi, >=20 > I want to do a check on a column if other columns meet certain conditions.= The program I'm working with allows to create additional columns on every '= object' - called extra data, but I have no control over the program. I want= to enforce the values on this one extra data to be of type date. >=20 > My idea was to do a Trigger function and cast to a date and if there's an e= xception, raise an error. Below is what I've tried, but it just keeps on Ra= ising Exception. >=20 > Could someone please help me? The date I enter is: 2012-10-10 which works f= ine if I do a: > select '2012-10-10'::date >=20 > Thanks >=20 > --Postgres 9.0 >=20 > CREATE OR REPLACE FUNCTION fnc_check_PO_extra_date() > RETURNS trigger AS > $BODY$ > DECLARE > tmp_date date; > BEGIN > IF (TG_OP =3D 'INSERT' OR TG_OP =3D 'UPDATE') THEN > IF (NEW.data_value IS NOT NULL or new.data_value =3D '') and NEW.extra_= id =3D > (select extra_id from extra_data where data_type =3D 9 > and (data_name =3D 'ETA' or data_name =3D 'Adjusted ETA')) > THEN > tmp_date :=3D new.data_value::date; > END IF; > END IF; > EXCEPTION > WHEN others THEN > RAISE EXCEPTION 'Invalid date on Extra Data!'; > return NEW; > END; > $BODY$ > LANGUAGE plpgsql VOLATILE >=20 > CREATE TRIGGER trg_check_PO_extra_date > BEFORE INSERT OR UPDATE > ON extra_values > FOR EACH ROW > EXECUTE PROCEDURE fnc_check_PO_extra_date(); >=20 >=20 You are suppressing the original exception so figuring out what is wrong is v= ery difficult. Your IF allows new.data_value to be the empty string which, iirc, cannot be= cast to date David J.