Received: from localhost (unknown [200.46.204.184]) by developer.postgresql.org (Postfix) with ESMTP id 710912E003A for ; Mon, 7 Apr 2008 09:11:31 -0300 (ADT) Received: from developer.postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.184]) (amavisd-maia, port 10024) with ESMTP id 23292-06 for ; Mon, 7 Apr 2008 09:11:18 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.5 Received: from fg-out-1718.google.com (fg-out-1718.google.com [72.14.220.158]) by developer.postgresql.org (Postfix) with ESMTP id 2667F2E0038 for ; Mon, 7 Apr 2008 09:11:24 -0300 (ADT) Received: by fg-out-1718.google.com with SMTP id 22so1199909fge.23 for ; Mon, 07 Apr 2008 05:11:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; bh=kv9oIwqUoavU2w9OU2l6BmCDOXbTb7K8XJlxP2ZoXts=; b=x8RGOp0ksleZRiJORGAheHAHz5MWOAJN1tQxm/UwQA6bFUPMkvis1st5Zh0xVobhl96yybjFg2w/NCVJz5dYVeifh/Lry3xigx+1h4fIgOXNcdLjbp6T64gRLGil6rU+tP8EEIjoexfcLTnEDtZ6C8I8I5QAP6g1+PQzrmI1jMY= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=EIxjMuUtF5J0n28XZG2jaBL9itDQ2ODJlmumtOOnEUt1RuqEXha+/LD4HAiHuI4MMivfn/Cf62w9BaLO+DZLpHywvCfrUs/5Rg/ZCt0MXeqTW3RAaJMmCUyd2rvU4C438I8bt8uKQRHlVnXwU/onG/5dqRgO+dOBVtYfiL+9du8= Received: by 10.86.57.9 with SMTP id f9mr3458961fga.28.1207570283271; Mon, 07 Apr 2008 05:11:23 -0700 (PDT) Received: by 10.86.80.3 with HTTP; Mon, 7 Apr 2008 05:11:23 -0700 (PDT) Message-ID: <162867790804070511y7266c911w370db97a78c3c519@mail.gmail.com> Date: Mon, 7 Apr 2008 14:11:23 +0200 From: "Pavel Stehule" To: "Martin Edlman" Subject: Re: pl/PgSQL, variable names in NEW Cc: pgsql-sql@postgresql.org In-Reply-To: <47FA034B.7090201@fortech.cz> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Content-Disposition: inline References: <47FA034B.7090201@fortech.cz> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200804/35 X-Sequence-Number: 30574 Hello no, it's not possible in plpgsql. Please, use plperl or plpython. Regards Pavel Stehule On 07/04/2008, Martin Edlman wrote: > Hello, > > is it possible to use variables as field names in the NEW record? > Let's suppose I have a varchar attname containg the name of the field and I > want to know a value that field of the NEW record. > > Problem is that I get an error 'record "new" has no field "attname"'. Of > course I want to use a value of NEW.author when col.attname = attname = > 'author'. > > Is there a solution? > > Example trigger function. It finds all columns in the table which are > referenced in other tables and checks if the value of the column has > changed. If yes, then invoke some other function. The problem is that the > column name is in the 'col' record and is different during the loop and at > each function call. > > CREATE OR REPLACE FUNCTION replace_values() RETURNS trigger AS > $BODY$ > DECLARE > col record; > attname varchar; > BEGIN > FOR col IN > SELECT DISTINCT pgaf.attname, pgaf.attnum > FROM pg_constraint, pg_attribute AS pgaf > WHERE pg_constraint.contype = 'f' -- fkey > AND pg_constraint.confrelid = TG_RELID -- table oid > AND pgaf.attrelid = TG_RELID > AND pgaf.attnum = ANY(pg_constraint.confkey) LOOP > > attname := col.attname; > IF NEW.attname <> OLD.attname THEN > RAISE NOTICE ' value changed from "%" to > "%"', OLD.attname, NEW.attname; > -- INVOKE OTHER FUNCTION > END IF; > END LOOP; > > END; > $BODY$ > LANGUAGE 'plpgsql' VOLATILE; > > -- > Martin Edlman > Fortech Ltd. > 57001 Litomysl, CZ > > -- > Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) > To make changes to your subscription: > http://www.postgresql.org/mailpref/pgsql-sql >