About a PL/pgSQL function - Mailing list pgsql-admin

From Ferdinand Smit
Subject About a PL/pgSQL function
Date
Msg-id 200206061738.51230.ferdinand@telegraafnet.nl
Whole thread Raw
Responses Re: About a PL/pgSQL function
List pgsql-admin
Hi,

I've created a little function, that returns an id and creates one if it does
not exist.


DROP FUNCTION fn(text,text);
CREATE OR REPLACE FUNCTION fn(text,text) RETURNS INT AS '
  DECLARE
    record_id INTEGER;
  BEGIN

    SELECT "id" INTO record_id FROM $1 WHERE def = $2 ;

    IF NOT FOUND THEN
      INSERT INTO $1 (def) VALUES($2);
      SELECT "id" INTO record_id FROM $1 WHERE def =  $2;
    END IF;

    RETURN record_id;
  END;
' LANGUAGE 'plpgsql';


When i run it i get:
NOTICE:  Error occurred while executing PL/pgSQL function fn
NOTICE:  line 5 at select into variables
ERROR:  parser: parse error at or near "$1"

When i create a function with a "static" table name, it works fine.
EXECUTE does not allow SELECT INTO, so does anyone have an other solution?

Ferdinand


pgsql-admin by date:

Previous
From: "Nicolas Nolst"
Date:
Subject: performance issue using DBI
Next
From: Joel Burton
Date:
Subject: Re: About a PL/pgSQL function