Re: sequence

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "cristi" <cristi(at)dmhi(dot)ct(dot)ro>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: sequence
Date: 2003-08-15 12:01:46
Message-ID: 12592.1060948906@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

"cristi" <cristi(at)dmhi(dot)ct(dot)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

In response to

  • sequence at 2003-08-15 09:32:36 from cristi

Browse pgsql-sql by date

  From Date Subject
Next Message Tim Andersen 2003-08-15 14:38:53 Re: About primary keys.
Previous Message Dennis Björklund 2003-08-15 10:40:28 Re: sequence