Skip site navigation (1) Skip section navigation (2)

Re: 'select nextval('seq_name');' in a function ?

From: Stephan Szabo <sszabo(at)megazone(dot)bigpanda(dot)com>
To: Pragati Kenkare <pkenkare(at)writeme(dot)com>
Cc: pgsql-novice(at)postgresql(dot)org, pgsql-sql(at)postgresql(dot)org
Subject: Re: 'select nextval('seq_name');' in a function ?
Date: 2004-01-31 15:23:36
Message-ID: (view raw, whole thread or download thread mbox)
Lists: pgsql-novicepgsql-sql
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.

In response to

pgsql-novice by date

Next:From: Stephan SzaboDate: 2004-01-31 15:50:20
Subject: Re: Method Question
Previous:From: Derrick BettsDate: 2004-01-31 07:18:38
Subject: Re: Emailing a list of users when a record is entered in the database.

pgsql-sql by date

Next:From: Stephan SzaboDate: 2004-01-31 15:26:01
Subject: Re: Mechanics of Update:Cascade
Previous:From: beyaRecords - The home Urban musicDate: 2004-01-31 10:50:03
Subject: Mechanics of Update:Cascade

Privacy Policy | About PostgreSQL
Copyright © 1996-2017 The PostgreSQL Global Development Group