Question about setval() function

Started by Erwin Ambroschalmost 24 years ago6 messagesgeneral
Jump to latest
#1Erwin Ambrosch
ambre@ebutec.com

Hi,

the following doesn't work.

SELECT setval('int_article_id_seq', SELECT max(id) FROM int_article);

I also tried to cast in the second argument, but with no success.

SELECT setval('int_article_id_seq', SELECT CAST(max(id) FROM int_article AS
INT4));

Is this not possible at all or do I anything wrong.

Tanks Erwin

#2Martijn van Oosterhout
kleptog@svana.org
In reply to: Erwin Ambrosch (#1)
Re: Question about setval() function

On Tue, May 14, 2002 at 04:18:47PM +0200, Erwin Ambrosch wrote:

Hi,

the following doesn't work.

SELECT setval('int_article_id_seq', SELECT max(id) FROM int_article);

I'm just guessing, but what if you put ()'s around the subquery?

HTH,
--
Martijn van Oosterhout <kleptog@svana.org> http://svana.org/kleptog/

Show quoted text

Canada, Mexico, and Australia form the Axis of Nations That
Are Actually Quite Nice But Secretly Have Nasty Thoughts About America

#3Tom Lane
tgl@sss.pgh.pa.us
In reply to: Erwin Ambrosch (#1)
Re: Question about setval() function

Erwin Ambrosch <ambre@ebutec.com> writes:

the following doesn't work.

SELECT setval('int_article_id_seq', SELECT max(id) FROM int_article);

You've got the syntax wrong: subselects must be parenthesized.

SELECT setval('int_article_id_seq', (SELECT max(id) FROM int_article));

regards, tom lane

#4scott.marlowe
scott.marlowe@ihs.com
In reply to: Erwin Ambrosch (#1)
Re: Question about setval() function

On Tue, 14 May 2002, Erwin Ambrosch wrote:

Hi,

the following doesn't work.

SELECT setval('int_article_id_seq', SELECT max(id) FROM int_article);

I also tried to cast in the second argument, but with no success.

SELECT setval('int_article_id_seq', SELECT CAST(max(id) FROM int_article AS
INT4));

You're doing it (just a little bit) wrong. sub selects should ALWAYS be
enclosed by () pairs, so...

SELECT setval('int_article_id_seq', (SELECT CAST(max(id) FROM int_article
AS INT4)));

should work (it does on my system).

#5Joel Burton
joel@joelburton.com
In reply to: Erwin Ambrosch (#1)
Re: Question about setval() function

-----Original Message-----
From: pgsql-general-owner@postgresql.org
[mailto:pgsql-general-owner@postgresql.org]On Behalf Of Erwin Ambrosch
Sent: Tuesday, May 14, 2002 10:19 AM
To: pgsql-general@postgresql.org
Subject: [GENERAL] Question about setval() function

the following doesn't work.

SELECT setval('int_article_id_seq', SELECT max(id) FROM int_article);

Nope. A select statement isn't itself a valid argument to a function. Turn
it into a subselect with parens:

SELECT setval('seq_name', ( SELECT max(id) FROM a_table ) );

- J.

Joel BURTON | joel@joelburton.com | joelburton.com | aim: wjoelburton
Knowledge Management & Technology Consultant

#6Tom Pfau
T.Pfau@emCrit.com
In reply to: Joel Burton (#5)
Re: Question about setval() function

-----Original Message-----
From: Erwin Ambrosch [mailto:ambre@ebutec.com]
Sent: Tuesday, May 14, 2002 10:19 AM
To: pgsql-general@postgresql.org
Subject: [GENERAL] Question about setval() function

the following doesn't work.

SELECT setval('int_article_id_seq', SELECT max(id) FROM int_article);

Try this instead:

SELECT setval('int_article_id_seq', max(id)) FROM int_article;