Re: Getting result from EXECUTE

From: "Sibte Abbas" <sibtay(at)gmail(dot)com>
To: robert(at)webtent(dot)com
Cc: PostgreSQL <pgsql-general(at)postgresql(dot)org>
Subject: Re: Getting result from EXECUTE
Date: 2007-09-09 20:45:29
Message-ID: bd6a35510709091345k4c7ecadh47229f3a7d633270@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 9/8/07, Robert Fitzpatrick <lists(at)webtent(dot)net> wrote:
>
> I have a trigger function that I want to apply to several tables, hence
> my use of TG_RELNAME. I just want the record to get inserted if an
> UPDATE comes from my view rule if the record for the client doesn't
> already exist. This is what I have, but I'm finding the FOUND is not
> returned for EXECUTE. How can I accomplish what I need?
>
> CREATE OR REPLACE FUNCTION "public"."create_fldclientnumber_trigg_func" ()
> RETURNS trigger AS'
> begin
> EXECUTE ''SELECT * FROM '' || TG_RELNAME || '' WHERE fldclientnumber =
> '' || NEW.fldclientnumber;
> IF NOT FOUND THEN
> EXECUTE ''INSERT INTO '' || TG_RELNAME || '' (fldclientnumber) VALUES
> ('' || NEW.fldclientnumber || '')'';
> END IF;
> RETURN NEW;
> end;
> 'LANGUAGE 'plpgsql' VOLATILE CALLED ON NULL INPUT SECURITY INVOKER;
>
> Thanks for the help.
>
> --
> Robert
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 2: Don't 'kill -9' the postmaster
>

AFAIK the FOUND variable does not get updated as part of an EXECUTE command.

Consider using a strict INTO clause alongwith EXECUTE. This way a
NO_DATA_FOUND exception will be generated if your query did'nt return any
data. Something like this:

DECLARE

v_rec record;
BEGIN

EXECUTE ''SELECT * FROM '' || TG_RELNAME || '' WHERE fldclientnumber = '' ||
NEW.fldclientnumber INTO STRICT v_rec;

EXCEPTION
when no_data_found then
/* do something */

regards,
--
Sibte Abbas

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Håkan Jacobsson 2007-09-09 21:40:11 Re: SQL for Deleting all duplicate entries
Previous Message Christian Schröder 2007-09-09 16:41:46 Re: Query with "like" is really slow