issue with SELECT settval(..);

Started by Alain Rogerabout 19 years ago4 messagesgeneral
Jump to latest
#1Alain Roger
raf.news@gmail.com

Hi,

I would like to auto increment my sequence when i'm going to insert a new
record into my table.

for that i wanted to use :

insert into immense.statususer (statususer_id, statususer_type) values
(SELECT nextval( 'statususer_statususer_id_seq' ),'customer');

however, i get an error message in pgAdmin III, as following :
ERROR: syntax error at or near "SELECT" at character 168

i do not under because if I run SELECT nextval(
'statususer_statususer_id_seq' ) alone, it works perfectly and increment the
sequence.
moreover, everything regarding the table is correct. so where is the problem
?

thanks a lot,

--
Alain
------------------------------------
Windows XP SP2
PostgreSQL 8.1.4
Apache 2.0.58
PHP 5

In reply to: Alain Roger (#1)
Re: issue with SELECT settval(..);

Alain Roger wrote:

insert into immense.statususer (statususer_id, statususer_type) values
(SELECT nextval( 'statususer_statususer_id_seq' ),'customer');

The correct syntax would be:

insert into immense.statususer (statususer_id, statususer_type) values
((SELECT nextval( 'statususer_statususer_id_seq' )),'customer');

The sub-select must be put in parentheses. However, the much simpler
statement

insert into immense.statususer (statususer_id, statususer_type) values
(nextval( 'statususer_statususer_id_seq' ),'customer');

will do the same without a sub-select.

Regards
Christian

--
Deriva GmbH Tel.: +49 551 489500-42
Financial IT and Consulting Fax: +49 551 489500-91
Hans-B�ckler-Stra�e 2 http://www.deriva.de
D-37079 G�ttingen

Deriva CA Certificate: http://www.deriva.de/deriva-ca.cer

#3Alain Roger
raf.news@gmail.com
In reply to: Christian Schröder (#2)
Re: issue with SELECT settval(..);

thanks a lot Christian.

On 3/18/07, Christian Schröder <cs@deriva.de> wrote:

Alain Roger wrote:

insert into immense.statususer (statususer_id, statususer_type) values
(SELECT nextval( 'statususer_statususer_id_seq' ),'customer');

The correct syntax would be:

insert into immense.statususer (statususer_id, statususer_type) values
((SELECT nextval( 'statususer_statususer_id_seq' )),'customer');

The sub-select must be put in parentheses. However, the much simpler
statement

insert into immense.statususer (statususer_id, statususer_type) values
(nextval( 'statususer_statususer_id_seq' ),'customer');

will do the same without a sub-select.

Regards
Christian

--
Deriva GmbH Tel.: +49 551 489500-42
Financial IT and Consulting Fax: +49 551 489500-91
Hans-Böckler-Straße 2 http://www.deriva.de
D-37079 Göttingen

Deriva CA Certificate: http://www.deriva.de/deriva-ca.cer

---------------------------(end of broadcast)---------------------------
TIP 1: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo@postgresql.org so that your
message can get through to the mailing list cleanly

--
Alain
------------------------------------
Windows XP SP2
PostgreSQL 8.1.4
Apache 2.0.58
PHP 5

#4Alban Hertroys
alban@magproductions.nl
In reply to: Christian Schröder (#2)
Re: issue with SELECT settval(..);

Christian Schröder wrote:

Alain Roger wrote:

insert into immense.statususer (statususer_id, statususer_type) values
(SELECT nextval( 'statususer_statususer_id_seq' ),'customer');

The correct syntax would be:

insert into immense.statususer (statususer_id, statususer_type) values
((SELECT nextval( 'statususer_statususer_id_seq' )),'customer');

Well, that original query was almost right, it just didn't need the
values statement and the parenthesis:

insert into immense.statususer (statususer_id, statususer_type)
SELECT nextval('statususer_statususer_id_seq'), 'customer';

But as mentioned; using nextval directly is better.
--
Alban Hertroys
alban@magproductions.nl

magproductions b.v.

T: ++31(0)534346874
F: ++31(0)534346876
M:
I: www.magproductions.nl
A: Postbus 416
7500 AK Enschede

// Integrate Your World //