From: Leif Biberg Kristensen <leif@solumslekt.org>
To: pgsql-sql@postgresql.org
Subject: Re: Subselects not allowed?
Date: Sat, 11 Jun 2011 17:39:04 +0200
Message-ID: <201106111739.04845.leif@solumslekt.org> (raw)
In-Reply-To: <20110611152340.GA9062@tux>
References: <201106111701.54145.leif@solumslekt.org>
<20110611152340.GA9062@tux>
On Saturday 11. June 2011 17.23.40 Andreas Kretschmer wrote:
> Leif Biberg Kristensen <leif@solumslekt.org> wrote:
> > Can anybody tell me why this doesn't work?
> >
> > pgslekt=> CREATE SEQUENCE sources_source_id_seq START WITH (SELECT
> > MAX(source_id) FROM sources);
> > ERROR: syntax error at or near "("
> > LINE 1: CREATE SEQUENCE sources_source_id_seq START WITH (SELECT MAX...
> >
> > ^
> >
> > pgslekt=>
> >
> > regards, Leif
>
> You can't do that, but you can do this:
>
> test=# select * from foo;
> i
> ----
> 98
> 99
> (2 rows)
>
> Time: 0,146 ms
> test=*# \ds seq_foo;
> No matching relations found.
> test=*# do $$ declare m int; begin select into m max(i) from foo; execute
> 'create sequence seq_foo start with ' || m; end; $$; DO
> Time: 1,115 ms
> test=*# \ds seq_foo;
> List of relations
> Schema | Name | Type | Owner
> --------+---------+----------+------------
> public | seq_foo | sequence | kretschmer
> (1 row)
>
> test=*# select * from seq_foo;
> sequence_name | last_value | start_value | increment_by | max_value
> | min_value | cache_value | log_cnt | is_cycled | is_called
> ---------------+------------+-------------+--------------+----------------
> -----+-----------+-------------+---------+-----------+----------- seq_foo
> | 99 | 99 | 1 | 9223372036854775807 |
> 1 | 1 | 1 | f | f (1 row)
>
>
>
> I'm using 9.1Beta, but it works since 9.0, see:
> http://www.depesz.com/index.php/2009/11/01/waiting-for-8-5-do/#more-1535
Yes it works like a charm with 9.x, but it's not backwards compatible.
That looks like a problem waiting to happen. But of course I can create a one-
shot function and drop it afterwards.
regards, Leif
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-sql@postgresql.org
Cc: leif@solumslekt.org
Subject: Re: Subselects not allowed?
In-Reply-To: <201106111739.04845.leif@solumslekt.org>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
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