Re: PL/pgSQL Function Problem

From: Doug McNaught <doug(at)mcnaught(dot)org>
To: the inquirer <listquestions(at)yahoo(dot)com>
Cc: pgsql-general(at)postgresql(dot)org, pgsql-novice(at)postgresql(dot)org, pgsql-sql(at)postgresql(dot)org
Subject: Re: PL/pgSQL Function Problem
Date: 2004-09-11 13:55:42
Message-ID: 87pt4sap0h.fsf@asmodeus.mcnaught.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-novice pgsql-sql

the inquirer <listquestions(at)yahoo(dot)com> writes:

> ERROR: syntax error at or near "$1" at character 14
> CONTEXT: PL/pgSQL function "create_author" line 7 at
> SQL statement

PL/pgSQL errors are sometimes obscure. I'll try to comment on what
looks wrong to me...

> CREATE OR REPLACE FUNCTION create_author (
> VARCHAR(32), VARCHAR(32), VARCHAR(32) )
> RETURNS INTEGER AS '
> DECLARE
> name_ ALIAS FOR $1;
> username_ ALIAS FOR $2;
> password_ ALIAS FOR $3;
> authorid_ INTEGER;
> BEGIN
> CREATE USER username_ WITH ENCRYPTED PASSWORD
> password_ IN GROUP authors;

I can't tell you why, but my suspicion is that you need to use EXECUTE
in order to do CREATE USER in a function. Try that.

> INSERT INTO Authors

Be aware that, unless you double-quote the identifier, "Author" will
be folded to "author" by the SQL parser. This may also be causing a
problem--you didn't post your schema.

> ( Name, Username )
> VALUES
> ( $1, $2 );
> SELECT Max( AuthorID ) INTO authorid_ FROM Authors;

This is a potential race, depending on your transaction isolation
level. It would be better to find out the sequence name for your
SERIAL column and user currval('<that sequence>') as the return value.

Hope this helps!

-Doug
--
Let us cross over the river, and rest under the shade of the trees.
--T. J. Jackson, 1863

In response to

Browse pgsql-general by date

  From Date Subject
Next Message William Herring 2004-09-11 14:04:54 table with a variable name???
Previous Message Doug McNaught 2004-09-11 13:43:39 Re: One Database per Data File?

Browse pgsql-novice by date

  From Date Subject
Next Message Ennio-Sr 2004-09-11 14:00:43 Re: iso-8859-2
Previous Message Tom Lane 2004-09-11 06:22:47 Re: forcing date ordering

Browse pgsql-sql by date

  From Date Subject
Next Message m0ntar3 2004-09-11 20:42:46 create unique index schema.index_name on table (column)?
Previous Message Michael Glaesemann 2004-09-11 12:57:23 Re: HOW TO HANDLE ZEROS IN DATE FIELD?