Received: from localhost (unknown [200.46.204.184]) by developer.postgresql.org (Postfix) with ESMTP id 6C7C32E0046 for ; Mon, 7 Apr 2008 08:29:46 -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 11901-03 for ; Mon, 7 Apr 2008 08:29:36 -0300 (ADT) X-Greylist: delayed 00:09:47.414416 by SQLgrey-1.7.5 Received: from mail.bezdrat.net (mail.bezdrat.net [213.250.192.15]) by developer.postgresql.org (Postfix) with ESMTP id 80D7F2E0041 for ; Mon, 7 Apr 2008 08:29:43 -0300 (ADT) Received: from localhost (localhost.localdomain [127.0.0.1]) by mail.bezdrat.net (Postfix) with ESMTP id 7D82EC129 for ; Mon, 7 Apr 2008 13:19:53 +0200 (CEST) X-Virus-Scanned: amavisd-new at mail.bezdrat.net Received: from mail.bezdrat.net ([127.0.0.1]) by localhost (mail.bezdrat.net [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id hYvArLSxRizw for ; Mon, 7 Apr 2008 13:19:52 +0200 (CEST) Received: from edas.lit.cz (unknown [213.250.198.35]) by mail.bezdrat.net (Postfix) with ESMTP id 976D3C118 for ; Mon, 7 Apr 2008 13:19:52 +0200 (CEST) Message-ID: <47FA034B.7090201@fortech.cz> Date: Mon, 07 Apr 2008 13:19:39 +0200 From: Martin Edlman User-Agent: Thunderbird 2.0.0.12 (X11/20080226) MIME-Version: 1.0 To: pgsql-sql@postgresql.org Subject: pl/PgSQL, variable names in NEW X-Enigmail-Version: 0.95.6 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests=none X-Spam-Level: X-Archive-Number: 200804/34 X-Sequence-Number: 30573 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