Re: 'order by' in an insert into command

From: Richard Huxton <dev(at)archonet(dot)com>
To: Mike Nolan <nolan(at)gw(dot)tssi(dot)com>
Cc: pgsql general list <pgsql-general(at)postgresql(dot)org>
Subject: Re: 'order by' in an insert into command
Date: 2004-09-08 16:46:41
Message-ID: 413F3771.2070800@archonet.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Mike Nolan wrote:
> I have the following insert to populate a new table:
>
> insert into pending_tnmt_sec
> select tseceventid, tsecsecno,
> nextval('sec_seq'),
> tsecrtddt
> from tnmtsec
> order by tsecrtddt,tseceventid,tsecsecno;
>
> I need to access this data in a particular order which may change over
> time but the initial order I want is in the order by clause.
>
> The problem is, I'm not getting the data into the right order based
> on the sequence values being inserted:

In your example, I would expect the nextval() to be called during the
"fetch", before the ordering. You could probably do something like:

INSERT INTO pending_tnmt_sec
SELECT foo.*, nextval('sec_seq') FROM
(
SELECT tseceventid, ...
ORDER BY tsecrtddt,tseceventid,tsecsecno
) AS foo
;

I'm not sure whether the SQL standard requires the ORDER BY to be
processed in the sub-select. From a relational viewpoint, I suppose you
could argue that ordering is strictly an output feature.

--
Richard Huxton
Archonet Ltd

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Jean-Luc Lachance 2004-09-08 16:47:20 Re: 'order by' in an insert into command
Previous Message UMPA Development 2004-09-08 16:40:52 Grant Issues with groups