About a PL/pgSQL function

Поиск
Список
Период
Сортировка
От Ferdinand Smit
Тема About a PL/pgSQL function
Дата
Msg-id 200206061738.51230.ferdinand@telegraafnet.nl
обсуждение исходный текст
Ответы Re: About a PL/pgSQL function  (Joel Burton <joel@joelburton.com>)
Список 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 по дате отправления:

Предыдущее
От: "Nicolas Nolst"
Дата:
Сообщение: performance issue using DBI
Следующее
От: Joel Burton
Дата:
Сообщение: Re: About a PL/pgSQL function