pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
About a PL/pgSQL function
2+ messages / 2 participants
[nested] [flat]

* About a PL/pgSQL function
@ 2002-06-06 15:38 Ferdinand Smit <ferdinand@telegraafnet.nl>
  2002-06-06 16:41 ` Re: About a PL/pgSQL function Joel Burton <joel@joelburton.com>
  0 siblings, 1 reply; 2+ messages in thread

From: Ferdinand Smit @ 2002-06-06 15:38 UTC (permalink / raw)
  To: 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




^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: About a PL/pgSQL function
  2002-06-06 15:38 About a PL/pgSQL function Ferdinand Smit <ferdinand@telegraafnet.nl>
@ 2002-06-06 16:41 ` Joel Burton <joel@joelburton.com>
  0 siblings, 0 replies; 2+ messages in thread

From: Joel Burton @ 2002-06-06 16:41 UTC (permalink / raw)
  To: Ferdinand Smit <ferdinand@telegraafnet.nl>; +Cc: pgsql-admin

On Thu, 6 Jun 2002, Ferdinand Smit wrote:

> 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?

Use:

DECLARE
  cursorname refcursor;
  varname int;
BEGIN
  OPEN cursorname FOR EXECUTE ''SELECT ... FROM '' || $1 || ...;
  FETCH cursorname INTO varname;
  CLOSE cursorname;
  RETURN varname;
END;


-- 

Joel BURTON  |  joel@joelburton.com  |  joelburton.com  |  aim: wjoelburton
Independent Knowledge Management Consultant




^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2002-06-06 16:41 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2002-06-06 15:38 About a PL/pgSQL function Ferdinand Smit <ferdinand@telegraafnet.nl>
2002-06-06 16:41 ` Joel Burton <joel@joelburton.com>

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