Subject Re: [ib-support] Order By in a Insert statement?
Author Douglas Tosi
Hi,

> >But this Doesn't:
> >INSERT INTO MG
> >SELECT *
> >FROM KS WHERE ZIP='66873' ORDER BY 3;
> >
> >Is this not allowed?
>
> There is a good chance this is not allowed since according to long
> established RDBMS principles the order records are in a table is not
> significant. According to that principle the order that the records are
> obtained from the source table is irrelevant if they are just being
> inserted into another table. I'm sure you can order the records later when
> they are subsequently selected from the destination table.

I also needed this, because the destination table had a couple of triggers
that did some data processing and it heavily depended on the order that the
records were inserted. It was real time data and had to be processed in the
exact order in which it arrived.
The solution? Write a stored proc such as:

FOR SELECT FIELDS
FROM MYTABLE
ORDER BY WHATEVER
INTO :VARIABLES DO
BEGIN
INSERT INTO OTHERTABLE(FIELDS) VALUES (:VARIABLES);
END

It may be a bit slower than a single insert, but does the job nicely.

hth,

dog