Skip site navigation (1) Skip section navigation (2)

Passing the table name as a function argument in PLPGSQL

From: Vincent Ficet <jean-vincent(dot)ficet(at)bull(dot)net>
To: pgsql novice <pgsql-novice(at)postgresql(dot)org>
Subject: Passing the table name as a function argument in PLPGSQL
Date: 2011-06-14 07:26:40
Message-ID: 4DF70D30.90303@bull.net (view raw or flat)
Thread:
Lists: pgsql-novice
Hello,


Is it possible to pass the name of the table to update to a given function ?

For example, how can I do something like:

CREATE FUNCTION compute_ipaddr(CHARACTER VARYING, INET)
    AS $_$
DECLARE
   tbl_name ALIAS FOR $1;
   module_ipaddr ALIAS FOR $2;
BEGIN
 UPDATE tbl_name SET ipaddr = module_ipaddr  ....

END;
$_$
    LANGUAGE PLPGSQL;


Since this does not work, I have to do

IF tbl_name = 'controller' THEN
  UPDATE controller SET ipaddr = module_ipaddr ...
ELSE IF tbl_name = 'server' THEN
  UPDATE server SET ipaddr = module_ipaddr ...
[ ...]


Is there a technique to pass the table name and so make the code more
generic ?

Thanks for your help

Vincent






Responses

pgsql-novice by date

Next:From: Maalini SamuganardanDate: 2011-06-14 08:59:13
Subject: Scheduling autovacuum
Previous:From: Jean-Yves F. BarbierDate: 2011-06-14 02:23:58
Subject: Re: permissions PB [SOLVED]

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group