From: Alex Hunsaker <badalex@gmail.com>
To: Josi Perez (3T Systems) <josiperez3t@gmail.com>
Cc: pgsql-admin@postgresql.org
Subject: Re: Trigger with dynamic SQL
Date: Mon, 24 May 2010 12:14:13 -0600
Message-ID: <AANLkTimEmuku6afAV1S_0Qp0uZAcdg5BwRTlYRPaccJO@mail.gmail.com> (raw)
In-Reply-To: <AANLkTikTOkORnSAEa0dEk5x0TtB-Gt3TJqwUR-IxmohS@mail.gmail.com>
References: <AANLkTikTOkORnSAEa0dEk5x0TtB-Gt3TJqwUR-IxmohS@mail.gmail.com>
On Wed, May 19, 2010 at 06:30, Josi Perez (3T Systems)
<josiperez3t@gmail.com> wrote:
> To avoid to delete registers I created one trigger activated "before delete"
> with lines like that:
> UPDATE tableX set dtExc = 'now', userExc = current_user where idTableX =
> OLD.idTableX;
> return NULL;
>
> but, I need do the same for many tables and I don't catch how.
> I created an sql variable to construct the update command using parameters
> on trigger
> qry := 'UPDATE '||arg_table||' set userexc = '
> ||chr(39)||current_user||chr(39)||', dtalt = '||'''now'''||' where ' ||
> arg_id ||' = OLD.'||TG_ARGV[1];
>
> but when "EXECUTE qry" I lost the OLD.variable.
Yeah, when the update runs its not running under pgpsql so referencing
OLD wont work. Try something like (note completely untested):
qry := 'UPDATE '|| quote_ident(arg_table) ||
' SET userexc = current_user, dtalt = now() where '||
quote_literal(arg_id) ||' = '|| quote_literal(OLD.TG_ARGV[1]) ||';';
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-admin@postgresql.org
Cc: badalex@gmail.com, josiperez3t@gmail.com
Subject: Re: Trigger with dynamic SQL
In-Reply-To: <AANLkTimEmuku6afAV1S_0Qp0uZAcdg5BwRTlYRPaccJO@mail.gmail.com>
References: <AANLkTikTOkORnSAEa0dEk5x0TtB-Gt3TJqwUR-IxmohS@mail.gmail.com> <AANLkTimEmuku6afAV1S_0Qp0uZAcdg5BwRTlYRPaccJO@mail.gmail.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox