pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Ferdinand Smit <ferdinand@telegraafnet.nl>
To: pgsql-admin@postgresql.org
Subject: About a PL/pgSQL function
Date: Thu, 6 Jun 2002 17:38:51 +0200
Message-ID: <200206061738.51230.ferdinand@telegraafnet.nl> (raw)

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




view thread (2+ messages)  latest in thread

Message-ID: <200206061738.51230.ferdinand@telegraafnet.nl>
Permalink:  ../200206061738.51230.ferdinand@telegraafnet.nl/
Also on:    postgresql.org/message-id/200206061738.51230.ferdinand@telegraafnet.nl

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-admin@postgresql.org
  Cc: ferdinand@telegraafnet.nl
  Subject: Re: About a PL/pgSQL function
  In-Reply-To: <200206061738.51230.ferdinand@telegraafnet.nl>
  References: <200206061738.51230.ferdinand@telegraafnet.nl>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox