pg.ddx.io  pgsql-php@postgresql.org mailing list archive  
help / color / mirror / Atom feed
functions with plpgsql
4+ messages / 3 participants
[nested] [flat]

* functions with plpgsql
@ 2003-08-09 20:18 Bernd Hoffmann <info@unixserver.info>
  2003-08-10 00:04 ` Re: functions with plpgsql Andrew J. Kopciuch <akopciuch@olympusproject.org>
  0 siblings, 1 reply; 4+ messages in thread

From: Bernd Hoffmann @ 2003-08-09 20:18 UTC (permalink / raw)
  To: pgsql-php

Hi,

I have some problems by using functions with plpgsql.

For example:

tbl:

CREATE TABLE "logs" (
   "id" int4 DEFAULT nextval('"logs_id_seq"'::text) NOT NULL,
   "session" varchar NOT NULL,
   "addr" inet NOT NULL,
   "host" varchar NOT NULL,
   "agent" varchar NOT NULL,
   "datum" timestamptz DEFAULT now(),
   "referer" varchar,
   CONSTRAINT "logs_pkey" PRIMARY KEY ("id")
);

function:

CREATE FUNCTION "next_id"(character varying) RETURNS integer AS '
DECLARE
    tabelle ALIAS FOR $1;
BEGIN
    SELECT MAX(id)+1 FROM tabelle;
END;
' LANGUAGE 'plpgsql' 

query:

SELECT next_id(logs);

error:

PostgreSQL meldet: ERROR: parser: parse error at or near "$1" 

Can anybody help me?

MfG

Bernd Hoffmann


unixserver.info
96123 Litzendorf
Tel: +499505/8050485
Fax: +499505/8050486
info@unixserver.info




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

* Re: functions with plpgsql
  2003-08-09 20:18 functions with plpgsql Bernd Hoffmann <info@unixserver.info>
@ 2003-08-10 00:04 ` Andrew J. Kopciuch <akopciuch@olympusproject.org>
  2003-08-10 00:33   ` Re: functions with plpgsql Bernd Hoffmann <info@unixserver.info>
  0 siblings, 1 reply; 4+ messages in thread

From: Andrew J. Kopciuch @ 2003-08-10 00:04 UTC (permalink / raw)
  To: Bernd Hoffmann <info@unixserver.info>; pgsql-php


> CREATE FUNCTION "next_id"(character varying) RETURNS integer AS '
> DECLARE
>     tabelle ALIAS FOR $1;
> BEGIN
>     SELECT MAX(id)+1 FROM tabelle;
> END;
> ' LANGUAGE 'plpgsql'
>
> query:
>
> SELECT next_id(logs);
>
> error:
>
> PostgreSQL meldet: ERROR: parser: parse error at or near "$1"
>
> Can anybody help me?


I do not think you can evaluate a declared vairable as a table name in the 
SELECT statement.

I don't think you really need to do this though.  If you have the table name 
already.

why go:

SELECT next_id(logs);

And write this functios instead of:

SELECT MAX(id) + 1 FROM logs;


??


Andy



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

* Re: functions with plpgsql
  2003-08-09 20:18 functions with plpgsql Bernd Hoffmann <info@unixserver.info>
  2003-08-10 00:04 ` Re: functions with plpgsql Andrew J. Kopciuch <akopciuch@olympusproject.org>
@ 2003-08-10 00:33   ` Bernd Hoffmann <info@unixserver.info>
  2003-08-10 04:53     ` Re: functions with plpgsql Brad Bulger <tater@potatoe.com>
  0 siblings, 1 reply; 4+ messages in thread

From: Bernd Hoffmann @ 2003-08-10 00:33 UTC (permalink / raw)
  To: pgsql-php

Hallo Andy,

> I do not think you can evaluate a declared vairable as a table name in the
> SELECT statement.

where can I find some docu about declare a table by variable.

> I don't think you really need to do this though.  If you have the table
name
> already.
> why go:
> SELECT next_id(logs);
> SELECT MAX(id) + 1 FROM logs;

it's only a example, I got everytime the error:

PostgreSQL meldet: ERROR: parser: parse error at or near "$1"

PS.: Excuse me, for my bad english :)

MfG

Bernd





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

* Re: functions with plpgsql
  2003-08-09 20:18 functions with plpgsql Bernd Hoffmann <info@unixserver.info>
  2003-08-10 00:04 ` Re: functions with plpgsql Andrew J. Kopciuch <akopciuch@olympusproject.org>
  2003-08-10 00:33   ` Re: functions with plpgsql Bernd Hoffmann <info@unixserver.info>
@ 2003-08-10 04:53     ` Brad Bulger <tater@potatoe.com>
  0 siblings, 0 replies; 4+ messages in thread

From: Brad Bulger @ 2003-08-10 04:53 UTC (permalink / raw)
  To: Bernd Hoffmann <info@unixserver.info>; pgsql-php



Bernd Hoffmann wrote:

> Hallo Andy,
> 
> 
>>I do not think you can evaluate a declared vairable as a table name in the
>>SELECT statement.
> 
> 
> where can I find some docu about declare a table by variable.

you need to do this as a dynamic query.

look in the pl/pgsql docs here
http://www.postgresql.org/docs/7.3/interactive/plpgsql-statements.html
and here
http://www.postgresql.org/docs/7.3/interactive/plpgsql-control-structures.html
for description of the FOR-IN-EXECUTE loop you need for dynamic SELECTs

basically it'll be something like

FOR myrecordvar IN EXECUTE ''select max(id) from '' || 
quote_ident(mytablenamevar) LOOP
	... do some stuff ...
END LOOP;





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


end of thread, other threads:[~2003-08-10 04:53 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2003-08-09 20:18 functions with plpgsql Bernd Hoffmann <info@unixserver.info>
2003-08-10 00:04 ` Andrew J. Kopciuch <akopciuch@olympusproject.org>
2003-08-10 00:33   ` Bernd Hoffmann <info@unixserver.info>
2003-08-10 04:53     ` Brad Bulger <tater@potatoe.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