Re: Help with plpgsql - subst variable value - Mailing list pgsql-general

From Stephan Szabo
Subject Re: Help with plpgsql - subst variable value
Date
Msg-id Pine.BSF.4.21.0102221115100.13173-100000@megazone23.bigpanda.com
Whole thread Raw
In response to Help with plpgsql - subst variable value  ("Alexander Bodnar" <bodnar@malva.com.ua>)
List pgsql-general
In 7.1, you should be able to use EXECUTE to execute an sql string.
You can basically build the create sequence string and use
execute to run it.  As a stripped down version, I did something like:

CREATE FUNCTION CreateObject(varchar(600)) RETURNS int4 AS '
  DECLARE
    ObjectSeqName text;
    ObjectID ALIAS FOR $1;
  BEGIN
     ObjectSeqName := ''ObjectLog'' || ObjectID || ''_Seq'';
     EXECUTE ''CREATE SEQUENCE '' || ObjectSeqName || '' start 1 increment 1 maxvalue 2147483647 minvalue 1 cache 1
CYCLE;'';
     return 1;
   END;
' LANGUAGE 'plpgsql';

When I executed it it created the sequence with the name I passed in.

On Thu, 22 Feb 2001, Alexander Bodnar wrote:

> Hi all.
>
> This my code snapshot:
>
> CREATE FUNCTION CreateObject(varchar(600)) RETURNS int4 AS '
>   DECLARE
>    . . .
>     ObjectSeqName text;
>   BEGIN
>   . . ..
>       ObjectSeqName := ''ObjectLog'' || ObjectID || ''_Seq'';
>      CREATE SEQUENCE ObjectSeqName start 1 increment 1 maxvalue 2147483647
> minvalue 1 cache 1 CYCLE;
>  . . .
>     END IF;
>     RETURN ObjectID;
>     END;
> ' LANGUAGE 'plpgsql';
>
> When I run this function I got
>    ERROR:  parser: parse error at or near "$1"
>
> So how can get value of ObjectSeqName in sequence/table/etc. name?
>
>
>



pgsql-general by date:

Previous
From: Peter Eisentraut
Date:
Subject: Re: Printing PostgreSQL reports
Next
From: newsreader@mediaone.net
Date:
Subject: Re: PostgreSQL 7.1 Linux Installation