Re: sequence - Mailing list pgsql-sql

From Tom Lane
Subject Re: sequence
Date
Msg-id 12592.1060948906@sss.pgh.pa.us
Whole thread Raw
In response to sequence  ("cristi" <cristi@dmhi.ct.ro>)
List pgsql-sql
"cristi" <cristi@dmhi.ct.ro> writes:
> What is wrong here?
> insert into table_name (field_name) values (select
> setval('sequence_name')-1) as currval);

Either too few parentheses, or too many ;-)

You could write this as an INSERT/SELECT:

insert into table_name (field_name) select setval('sequence_name')-1 as currval;

or you could write it as an INSERT/VALUES with scalar subquery
expression:

insert into table_name (field_name) values ((select setval('sequence_name')-1 as currval));

(all the parentheses are required here).  But really you do not need
a subquery for this at all; VALUES is perfectly content with scalar
expressions:

insert into table_name (field_name) values (setval('sequence_name')-1);
        regards, tom lane


pgsql-sql by date:

Previous
From: Bertrand Petit
Date:
Subject: Re: sequence
Next
From: Tim Andersen
Date:
Subject: Re: About primary keys.