Обсуждение: Question about setval() function
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
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/
> Canada, Mexico, and Australia form the Axis of Nations That
> Are Actually Quite Nice But Secretly Have Nasty Thoughts About America
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
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).
> -----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
-----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;