Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id 62261633037 for ; Mon, 24 May 2010 15:14:42 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 85182-01 for ; Mon, 24 May 2010 18:14:35 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-fx0-f46.google.com (mail-fx0-f46.google.com [209.85.161.46]) by mail.postgresql.org (Postfix) with ESMTP id E924D635FB7 for ; Mon, 24 May 2010 15:14:34 -0300 (ADT) Received: by fxm5 with SMTP id 5so2530738fxm.19 for ; Mon, 24 May 2010 11:14:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:mime-version:received:in-reply-to :references:from:date:message-id:subject:to:cc:content-type :content-transfer-encoding; bh=M6xy2Q4DXtyiZ9uzGa3Won2fvLk6nXdzoux9+sVtPzg=; b=mLzt0gl4A1/l8Z4Z2nspaPizyI/Haq7l5t5vQeaVgXFZhq5HoSuQm/XYA3G3dfWZRm OYJUEriZ8y17YrbwNqnU2gFgRR3A7D1HqNZDiQcc2ciubIfutMhlWtezag1oE/SiQNND ein7pMdV+hok5eCzSMEsKcXhnUzAc/FSAYvAg= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type:content-transfer-encoding; b=uuABltVh3A+zbJnNPfrSbpzuehpITWQ0/TCpRrdqvFDr0N+UwC7PDNraiVSS+EhJvi YmHYakMeOQoOKHTb2+GkIk8sN2q9bHOivRRQ0GLe4PzzFi3wqnxgXDcFzG6BYL1+Tudv uKW7J8BVJeZ94QkBAWRymSJmyFgFI1ooFO1CM= Received: by 10.223.4.217 with SMTP id 25mr4945512fas.60.1274724873223; Mon, 24 May 2010 11:14:33 -0700 (PDT) MIME-Version: 1.0 Received: by 10.223.123.212 with HTTP; Mon, 24 May 2010 11:14:13 -0700 (PDT) In-Reply-To: References: From: Alex Hunsaker Date: Mon, 24 May 2010 12:14:13 -0600 Message-ID: Subject: Re: Trigger with dynamic SQL To: "Josi Perez (3T Systems)" Cc: pgsql-admin@postgresql.org Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.9 tagged_above=-10 required=5 tests=BAYES_00=-1.9 X-Spam-Level: X-Archive-Number: 201005/223 X-Sequence-Number: 34696 On Wed, May 19, 2010 at 06:30, Josi Perez (3T Systems) wrote: > To avoid to delete registers I created one trigger activated "before dele= te" > with lines like that: > UPDATE tableX=C2=A0 set dtExc =3D 'now', userExc =3D current_user where i= dTableX =3D > 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 parameter= s > on trigger > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 qry :=3D 'UPDATE '||arg_table||' set usere= xc =3D ' > ||chr(39)||current_user||chr(39)||', dtalt =3D '||'''now'''||' where ' || > arg_id ||' =3D 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 :=3D 'UPDATE '|| quote_ident(arg_table) || ' SET userexc =3D current_user, dtalt =3D now() where '|| quote_literal(arg_id) ||' =3D '|| quote_literal(OLD.TG_ARGV[1]) ||';';