function to update different tables - Mailing list pgsql-novice

From Lars Eckberg
Subject function to update different tables
Date
Msg-id 20041103112919.M58332@eckbergconsulting.com
Whole thread Raw
Responses Re: function to update different tables  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-novice
Hey all,

Newbie question:

I'm trying to create a function, where the first parameter will be the
tablename to update. I have tried to escape $1 in every way I can think of,
but with no luck. I get an error when trying to create the function.

ERROR:  parser: parse error at or near "$1" at character 11

Here is the function:

CREATE FUNCTION reserve_ports(text,smallint,text,text,int) RETURNS smallint AS
'
  UPDATE $1
    SET status = \'reserved\', vlan_uid = $2, c_uid = $3
    WHERE status = \'free\' AND oid IN (SELECT oid FROM $1 WHERE  status =
\'free\' AND module_type ILIKE $4 ORDER BY id LIMIT $5);
  SELECT 1;
' LANGUAGE SQL;

I have looked at the manual but i cant seem to find anything covering this.
Do i have to use another approach??

Any examples or hints will be appreciated greatly. =)

Cheers,
--Lars


pgsql-novice by date:

Previous
From: ionFreeman@gmail.com (Ion Chalmers Freeman)
Date:
Subject: PREPARE function
Next
From: Tom Lane
Date:
Subject: Re: function to update different tables