By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,611 Members | 2,296 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,611 IT Pros & Developers. It's quick & easy.

Order by not working on select into GTT

P: n/a
I am executing the following code below. When we execute this on our
TEST system (Redhat 4.0, DB2 V8.2 FP11) the rows are returned in order
of dollar_range_from. We just moved our Production system from V7.2
(Redhat 2.1) to a
RedHat 4.0 64-bit/Db2 V8.2 64-bit FP14 enviromnment and now the below
code returns the rows out of order from the temp table even though
they were inserted with an order by.

DECLARE GLOBAL TEMPORARY TABLE session.TEMPTABLE ( dollar_range_from
DECIMAL(15,2), dollar_range_to DECIMAL(15,2) )
ON COMMIT PRESERVE ROWS NOT LOGGED ;

insert into session.TEMPTABLE
SELECT from_amount as dollar_range_from, to_amount as
dollar_range_to FROM prodsys.dollar_range_bands WHERE
dollar_range_type = 'T' AND report_ids = 'REPORT3' AND coverage_type =
'99' OrDER BY by dollar_range_from;
select * from session.temptable;

Mar 9 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
ebsch94 wrote:
I am executing the following code below. When we execute this on our
TEST system (Redhat 4.0, DB2 V8.2 FP11) the rows are returned in order
of dollar_range_from. We just moved our Production system from V7.2
(Redhat 2.1) to a
RedHat 4.0 64-bit/Db2 V8.2 64-bit FP14 enviromnment and now the below
code returns the rows out of order from the temp table even though
they were inserted with an order by.
The DBMS (in this case db2) will retrive rows in what order it finds
most appropiate for the moment, unless you explicitly specify an order.
This is how it is supposed to work. I'm a bit surprised that you can do
insert with an order by, since it is has no meaning. You should move
the "order by" so that it becomes part of the select instead of the insert

/Lennart
>

DECLARE GLOBAL TEMPORARY TABLE session.TEMPTABLE ( dollar_range_from
DECIMAL(15,2), dollar_range_to DECIMAL(15,2) )
ON COMMIT PRESERVE ROWS NOT LOGGED ;

insert into session.TEMPTABLE
SELECT from_amount as dollar_range_from, to_amount as
dollar_range_to FROM prodsys.dollar_range_bands WHERE
dollar_range_type = 'T' AND report_ids = 'REPORT3' AND coverage_type =
'99' OrDER BY by dollar_range_from;
select * from session.temptable;
Mar 9 '07 #2

P: n/a
On Mar 9, 3:10 pm, Lennart <erik.lennart.jons...@gmail.comwrote:
ebsch94 wrote:
I am executing the following code below. When we execute this on our
TEST system (Redhat 4.0, DB2 V8.2 FP11) the rows are returned in order
of dollar_range_from. We just moved our Production system from V7.2
(Redhat 2.1) to a
RedHat 4.0 64-bit/Db2 V8.2 64-bit FP14 enviromnment and now the below
code returns the rows out of order from the temp table even though
they were inserted with an order by.

The DBMS (in this case db2) will retrive rows in what order it finds
most appropiate for the moment, unless you explicitly specify an order.
This is how it is supposed to work. I'm a bit surprised that you can do
insert with an order by, since it is has no meaning. You should move
the "order by" so that it becomes part of the select instead of the insert

/Lennart


DECLARE GLOBAL TEMPORARY TABLE session.TEMPTABLE ( dollar_range_from
DECIMAL(15,2), dollar_range_to DECIMAL(15,2) )
ON COMMIT PRESERVE ROWS NOT LOGGED ;
insert into session.TEMPTABLE
SELECT from_amount as dollar_range_from, to_amount as
dollar_range_to FROM prodsys.dollar_range_bands WHERE
dollar_range_type = 'T' AND report_ids = 'REPORT3' AND coverage_type =
'99' OrDER BY by dollar_range_from;
select * from session.temptable;- Hide quoted text -

- Show quoted text -
It seems that in our QA system, and prior production system these rows
were returned in order of dollar_range_from, and unfortunately our
developer does other operations before the final select that depends
on those rows being in order. He is convinced something is wrong
because it behaves differently in our new production system.
Mar 9 '07 #3

P: n/a
ebsch94 wrote:
[...]
>
It seems that in our QA system, and prior production system these rows
were returned in order of dollar_range_from, and unfortunately our
developer does other operations before the final select that depends
on those rows being in order.
You've lost me here. How are the rows suposed to be in any order before
you retrieved them?
He is convinced something is wrong
because it behaves differently in our new production system.
A table is a set (actually a bag in sql), Therefor unordered by
definition. If rows where retrieved in a particular order before, it
could depend of a lot of things (luck, internal implementation, etc).
However, you should never ever expect them to do so (as you have noticed
by now).

If it where any other way you should be able to find a reference or two
that says that it is safe to assume that the rows are returned in a
certain order without using an order by. I dont think you will find one.

/Lennart
Mar 9 '07 #4

P: n/a
ebsch94 wrote:
[...]
>
It seems that in our QA system, and prior production system these rows
were returned in order of dollar_range_from, and unfortunately our
developer does other operations before the final select that depends
on those rows being in order.
You've lost me here. How are the rows suposed to be in any order before
you retrieved them?
He is convinced something is wrong
because it behaves differently in our new production system.
A table is a set (actually a bag in sql), Therefor unordered by
definition. If rows where retrieved in a particular order before, it
could depend of a lot of things (luck, internal implementation, etc).
However, you should never ever expect them to do so (as you have noticed
by now).

If it where any other way you should be able to find a reference or two
that says that it is safe to assume that the rows are returned in a
certain order without using an order by. I dont think you will find one.

/Lennart
Mar 9 '07 #5

P: n/a
Lennart wrote:
If it where any other way you should be able to find a reference or two
that says that it is safe to assume that the rows are returned in a
certain order without using an order by. I dont think you will find one.
If you do, please report it because that's a bug.

--
Knut Stolze
DB2 z/OS Admin Enablement
IBM Germany
Mar 12 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.