Re: My very first PL/pgSQL procedure...

From: "Jim Buttafuoco" <jim(at)contactbda(dot)com>
To: Philippe Ferreira <phil(dot)f(at)worldonline(dot)fr>, pgsql-general(at)postgresql(dot)org
Subject: Re: My very first PL/pgSQL procedure...
Date: 2006-01-25 17:58:44
Message-ID: 20060125175736.M14367@contactbda.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general


you need to use EXECUTE to do the dynamic lock table.

sql = 'LOCK TABLE ' || sequence_name || 'IN ACCESS EXCLUSIVE MODE';
EXECUTE sql;

---------- Original Message -----------
From: Philippe Ferreira <phil(dot)f(at)worldonline(dot)fr>
To: pgsql-general(at)postgresql(dot)org
Sent: Wed, 25 Jan 2006 18:37:21 +0100
Subject: [GENERAL] My very first PL/pgSQL procedure...

> Hi,
>
> I've got a problem with my very first PL/pgSQL procedure !
> I created the following procedure, that should reconfigure a sequence :
> ---------------------------------------------------------------------------------------------------------------------
>
> CREATE OR REPLACE FUNCTION seq_min(sequence_name varchar, minval
> integer) RETURNS VOID AS $$
> DECLARE
> current_seq integer;
> BEGIN
> LOCK TABLE sequence_name IN ACCESS EXCLUSIVE MODE;
> current_seq := last_value FROM sequence_name;
> IF current_seq < minval THEN
> ALTER SEQUENCE sequence_name RESTART WITH minval;
> END IF;
> END;
> $$ LANGUAGE plpgsql;
> ---------------------------------------------------------------------------------------------------------------------
>
> I call it from the psql interface by :
> SELECT seq_min('seq_mytable', 1029);
>
> But PostgreSQL returns the following error (translated from french) :
>
> ERROR: syntax error on or near «$1» at character 13
> QUERY : LOCK TABLE $1 IN ACCESS EXCLUSIVE MODE
> CONTEXT : PL/pgSQL function "seq_min" line 4 at SQL statement
> LINE 1 : LOCK TABLE $1 IN ACCESS EXCLUSIVE MODE
>
> So it seems that PostgreSQL have troubles handling my variable
> "sequence_name"...
> Any idea ?
>
> Thank you in advance,
> Philippe Ferreira, France.
>
> ---------------------------(end of broadcast)---------------------------
> TIP 3: Have you checked our extensive FAQ?
>
> http://www.postgresql.org/docs/faq
------- End of Original Message -------

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message gianni.zammarchi@tiscali.it 2006-01-25 18:05:19 xml support on win32 postgres
Previous Message MargaretGillon 2006-01-25 17:55:58 Re: Constraint that compares and limits field values