'select nextval('seq_name');' in a function ?
'select nextval('seq_name');' in a function ?
От:
"Pragati Kenkare" <pkenkare@writeme.com>
Дата:
Hi,
I am new to postgresql. Using PostgreSQL 7.3.2, I did the following.
testdb#CREATE SEQUENCE principal_id increment 1 start 1000 cache 5;
testdb#CREATE TABLE principal (principal_id int not null, name text, constraint pk_principal primary key(principal_id));
testdb#CREATE FUNCTION getnext_principal_id(int) returns int as 'select nextval('principal_id');' language 'SQL';
This gives ->
ERROR: parser: parse error at or near "principal_id" at character ...
Please help. Basically, I want to write equivalent of the following Oracle procedure:
create or replace procedure getnext_principal_id (retVal out principal.principal_id%TYPE ) as
begin
select principal_id.nextval into retValue from dual;
end;
Thanks much,
Pragati
--
_______________________________________________
Get your free email from http://www.iname.com
Re: 'select nextval('seq_name');' in a function ?
От:
Stephan Szabo <sszabo@megazone.bigpanda.com>
Дата:
On Sat, 24 Jan 2004, Pragati Kenkare wrote:
> I am new to postgresql. Using PostgreSQL 7.3.2, I did the following.
>
> testdb#CREATE SEQUENCE principal_id increment 1 start 1000 cache 5;
>
> testdb#CREATE TABLE principal (principal_id int not null, name text, constraint pk_principal primary key(principal_id));
>
> testdb#CREATE FUNCTION getnext_principal_id(int) returns int as 'select nextval('principal_id');' language 'SQL';
You need to double the single quotes around principal_id, and I'm not sure
which version switched to int8 sequences, and do you really want to be
passing in an unused int?
CREATE FUNCTION getnext_principal_id() returns int as 'select
CAST(nextval(''principal_id'') AS int4);' language 'SQL';
However, how are you planning to use this? Perhaps making principal_id a
serial would be better for you.