Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Data Bases > Pgsql Sql > pl/PgSQL, varia...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 5 Topic 3393 of 3596
Post > Topic >>

pl/PgSQL, variable names in NEW

by edlman@[EMAIL PROTECTED] (Martin Edlman) Apr 7, 2008 at 01:19 PM

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@[EMAIL PROTECTED]
)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql
 




 5 Posts in Topic:
pl/PgSQL, variable names in NEW
edlman@[EMAIL PROTECTED]   2008-04-07 13:19:39 
Re: pl/PgSQL, variable names in NEW
pavel.stehule@[EMAIL PROT  2008-04-07 14:11:23 
Re: pl/PgSQL, variable names in NEW
edlman@[EMAIL PROTECTED]   2008-04-08 13:06:47 
Re: pl/PgSQL, variable names in NEW
alvherre@[EMAIL PROTECTED  2008-04-08 09:21:57 
Re: pl/PgSQL, variable names in NEW
edlman@[EMAIL PROTECTED]   2008-04-10 13:34:06 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan13V112 Thu Jul 24 5:05:31 CDT 2008.