pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
sequence
4+ messages / 4 participants
[nested] [flat]

* sequence
@ 2003-08-15 09:32 cristi <cristi@dmhi.ct.ro>
  2003-08-15 10:30 ` Re: sequence Bertrand Petit <pgsql-sql@phoe.frmug.org>
  2003-08-15 10:40 ` Re: sequence Dennis Björklund <db@zigo.dhs.org>
  2003-08-15 12:01 ` Re: sequence Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 3 replies; 4+ messages in thread

From: cristi @ 2003-08-15 09:32 UTC (permalink / raw)
  To: pgsql-sql

What is wrong here?

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





^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: sequence
  2003-08-15 09:32 sequence cristi <cristi@dmhi.ct.ro>
@ 2003-08-15 10:30 ` Bertrand Petit <pgsql-sql@phoe.frmug.org>
  2 siblings, 0 replies; 4+ messages in thread

From: Bertrand Petit @ 2003-08-15 10:30 UTC (permalink / raw)
  To: cristi <cristi@dmhi.ct.ro>; +Cc: pgsql-sql

On Fri, Aug 15, 2003 at 12:32:36PM +0300, cristi wrote:
> What is wrong here?
> 
> insert into table_name (field_name) values (select
> setval('sequence_name')-1) as currval);

	This should be better:

	INSERT INTO table_name (field_name) VALUES (nextval('sequence_name'));

-- 
%!PS
297.6 420.9 translate 90 rotate 0 setgray gsave 0 1 1{pop 0 180 moveto 100
180 170 100 170 -10 curveto 180 -9 180 -9 190 -10 curveto 190 100 100 180
0 180 curveto fill 180 rotate}for grestore/Bookman-LightItalic findfont
240 scalefont setfont -151.536392 -63.7998886 moveto (bp)show showpage



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: sequence
  2003-08-15 09:32 sequence cristi <cristi@dmhi.ct.ro>
@ 2003-08-15 10:40 ` Dennis Björklund <db@zigo.dhs.org>
  2 siblings, 0 replies; 4+ messages in thread

From: Dennis Björklund @ 2003-08-15 10:40 UTC (permalink / raw)
  To: cristi <cristi@dmhi.ct.ro>; +Cc: pgsql-sql

On Fri, 15 Aug 2003, cristi wrote:

> What is wrong here?
> 
> insert into table_name (field_name) values (select
> setval('sequence_name')-1) as currval);

Your probably want this instead:

  insert into table_name (field_name) values (nextval('sequence_name'));

The reason why your insert fail above is that setval() should have more 
parameters, but even if it had worked it does not make sense to call 
setval() there. See

  http://www.postgresql.org/docs/7.3/static/functions-sequence.html

Also, it's easier to use a serial column:

  http://www.postgresql.org/docs/7.3/static/datatype.html#DATATYPE-SERIAL

then you can do

  insert into table_name (field_name) values (DEFAULT);

-- 
/Dennis




^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: sequence
  2003-08-15 09:32 sequence cristi <cristi@dmhi.ct.ro>
@ 2003-08-15 12:01 ` Tom Lane <tgl@sss.pgh.pa.us>
  2 siblings, 0 replies; 4+ messages in thread

From: Tom Lane @ 2003-08-15 12:01 UTC (permalink / raw)
  To: cristi <cristi@dmhi.ct.ro>; +Cc: 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



^ permalink  raw  reply  [nested|flat] 4+ messages in thread


end of thread, other threads:[~2003-08-15 12:01 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2003-08-15 09:32 sequence cristi <cristi@dmhi.ct.ro>
2003-08-15 10:30 ` Bertrand Petit <pgsql-sql@phoe.frmug.org>
2003-08-15 10:40 ` Dennis Björklund <db@zigo.dhs.org>
2003-08-15 12:01 ` Tom Lane <tgl@sss.pgh.pa.us>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox