473,757 Members | 6,899 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ORDERING by a FUNCTION() with simple-integer

An ORDER BY a simple-integer inside a FUNCTION, results in SQL0440N,
unless the FUNCTION expects an INTEGER as its parameter. For example:

DECLARE GLOBAL TEMPORARY TABLE A(A CHAR(1))
INSERT INTO SESSION.A VALUES ('a'), ('b')
CREATE FUNCTION A(A char(1)) RETURNS char(1) DETERMINISTIC NO EXTERNAL
ACTION RETURN A
SELECT A FROM SESSION.A ORDER BY A(1)
DROP FUNCTION A
DROP TABLE SESSION.A

Is there a way to pass a simple-integer inside a FUNCTION of an ORDER
BY clause?

B.

Dec 12 '05 #1
20 2499

"Brian Tkatch" <Ma***********@ ThePentagon.com > wrote in message
news:11******** *************@g 43g2000cwa.goog legroups.com...
An ORDER BY a simple-integer inside a FUNCTION, results in SQL0440N,
unless the FUNCTION expects an INTEGER as its parameter. For example:

DECLARE GLOBAL TEMPORARY TABLE A(A CHAR(1))
INSERT INTO SESSION.A VALUES ('a'), ('b')
CREATE FUNCTION A(A char(1)) RETURNS char(1) DETERMINISTIC NO EXTERNAL
ACTION RETURN A
SELECT A FROM SESSION.A ORDER BY A(1)
DROP FUNCTION A
DROP TABLE SESSION.A

Is there a way to pass a simple-integer inside a FUNCTION of an ORDER
BY clause?

I don't understand the SELECT in your function. What is "ORDER BY A(1)"
supposed to do? I wonder if that odd syntax is keeping your function from
working.... Does the function work better if you write 'ORDER BY A'?

Rhino
Dec 12 '05 #2
The reason of SQL0440N is that a parameter of your function is CHAR(1).
While you passed integer 1.
So, DB2 said there is no such function(Name is 'A' and parameter is
integer).

Dec 13 '05 #3
Brian Tkatch wrote:
An ORDER BY a simple-integer inside a FUNCTION, results in SQL0440N,
unless the FUNCTION expects an INTEGER as its parameter. For example:

DECLARE GLOBAL TEMPORARY TABLE A(A CHAR(1))
INSERT INTO SESSION.A VALUES ('a'), ('b')
CREATE FUNCTION A(A char(1)) RETURNS char(1) DETERMINISTIC NO EXTERNAL
ACTION RETURN A
SELECT A FROM SESSION.A ORDER BY A(1)
DROP FUNCTION A
DROP TABLE SESSION.A

Is there a way to pass a simple-integer inside a FUNCTION of an ORDER
BY clause?


What is that supposed to be doing? Remember: SQL (and the relational model
it is based on) is set-oriented. And the elements of sets have - per
definition - no ordering. So it doesn't make any sense to add an ORDER BY
inside the function as you're trying to do.

You can use an ORDER BY if you add something like a FETCH FIRST n ROWS ONLY
because the ordering does matter in this case. Otherwise, the only place
where an ORDER BY has an influence on the sorting of the rows returned to
the client is the outer-most full-select. Note that you can nest an ORDER
BY into sub-selects, but that will only have an effect if the outer-most
select uses an ORDER BY ORDER OF <correlation-name>

--
Knut Stolze
DB2 Information Integration Development
IBM Germany
Dec 13 '05 #4
> What is "ORDER BY A(1)" supposed to do?

It is supposed to ORDER the rows based on the output of the FUNCTION,
rather than the actual COLUMN itself.

B.

Dec 13 '05 #5
ORDER BY accepts a simple-integer as a parameter, which references the
column-order in the select-list. Therefore, the 1 should be treated as
a COLUMN references and not a number.

Further, being the FUNCTION must be DETERMINISTIC to be in the ORDER BY
clause, passing a literal would be useless.

B.

Dec 13 '05 #6
Brian Tkatch wrote:
ORDER BY accepts a simple-integer as a parameter, which references the
column-order in the select-list. Therefore, the 1 should be treated as
a COLUMN references and not a number.
That's not the case. Have a look at the syntax diagram for the
<order-by-clause>: http://tinyurl.com/9vdcc

You will find that the numeric value is a "simple-integer" only. When you
use the function, you are automatically in the branch "sort-key-expression"
and that doesn't handle simple-integers at all.
Further, being the FUNCTION must be DETERMINISTIC to be in the ORDER BY
clause, passing a literal would be useless.


No, it doesn't have to. At least there is no such restriction in DB2.
However, a non-deterministic function might give you unpredictable results.
But also note that any function that accesses (some) special registers like
CURRENT SCHEMA must be defined as NOT DETERMINISTIC, even if it is
deterministic within the scope of the current query.

--
Knut Stolze
DB2 Information Integration Development
IBM Germany
Dec 13 '05 #7
Brian Tkatch wrote:
ORDER BY accepts a simple-integer as a parameter, which references the
column-order in the select-list. Therefore, the 1 should be treated as
a COLUMN references and not a number.


Personally, I think that allowing those simple integers in the ORDER BY
clause was one of the worst ideas of the standardization committee. It
leaves the relational model behind (or ahead, depending on your view), and
it is not applied consistently, for example you can't do the same in a
GROUP BY. I would not use such a construct because it is only the cause
for confusions like the one we're just discussion now.

--
Knut Stolze
DB2 Information Integration Development
IBM Germany
Dec 13 '05 #8
>What is that supposed to be doing?

ORDERing the output BY the output of the FUNCTION.
And the elements of sets have - per definition - no ordering.
....which is why databases added a clause called ORDER BY. So that
ordering is possible.
So it doesn't make any sense to add an ORDER BY inside the function as you're trying to do.
Being i want it ORDERed BY the output of the FUNCTION, it makes a great
deal of sense.
Otherwise, the only place where an ORDER BY has an influence on the
sorting of the rows returned to the client is the outer-most full-select.
It is on the outer-most full-select.
Note that you can nest an ORDER BY into sub-selects, but that will only have an effect if the outer-most
select uses an ORDER BY ORDER OF <correlation-name>


That's way too confusing.

As a workaround i added the FUNCTION to the output list itself, and
ORDERed BY that COLUMN.

B.

Dec 13 '05 #9
Brian Tkatch wrote:
Otherwise, the only place where an ORDER BY has an influence on the
sorting of the rows returned to the client is the outer-most full-select.
It is on the outer-most full-select.


Oops, I thought your ORDER BY was inside the function. Sorry!
Note that you can nest an ORDER BY into sub-selects, but that will only
have an effect if the outer-most select uses an ORDER BY ORDER OF
<correlatio n-name>


That's way too confusing.


Actually, it is quite obvious:

SELECT ...
FROM ( SELECT ...
FROM ...
WHERE ...
ORDER BY ... ) AS t(...)
WHERE ...
ORDER BY ORDER OF t
This comes in extremely handy for export tools that take a whole sub-select
as input and if you need to add some more logic. For example, in the
Spatial Extender export routines it is necessary to extract some
information about the coordinate system in addition to the actual data that
needs to be exported. So the logic gets the select statement of the data
that shall be exported and then does something like this with it:

SELECT <column-list>, <spatial-column>..ST_Srs Id()
FROM ( <given-subselect> ) AS x(<column-list>)
ORDER BY ORDER OF x

That way, no additional scan is necessary to call the ST_SrsId() function
(it is done on the fly), and it is also possibly to retain the
user-intended ordering for the exported data.
As a workaround i added the FUNCTION to the output list itself, and
ORDERed BY that COLUMN.


You can simply give the function in the ORDER BY the parameter that it
expects, i.e. values of type CHAR(1) that are stored in the column:

SELECT a
FROM a
ORDER BY A(a)

--
Knut Stolze
DB2 Information Integration Development
IBM Germany
Dec 13 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2248
by: Jeff Thies | last post by:
I have 10 select boxes, they have select values of 1 to 10 (one of each value). I'd like to be able to change one of the select boxes and have the rest reorder themselves accordingly. I thought this would be simple, as the values of the other select boxes are either incremented up (if going from a smaller value to a larger) or incremented down over the range of the old value to (and including) the new value.
15
1746
by: Kay Schluehr | last post by:
Here might be an interesting puzzle for people who like sorting algorithms ( and no I'm not a student anymore and the problem is not a students 'homework' but a particular question associated with a computer algebra system in Python I'm currently developing in my sparetime ). For motivation lets define some expression class first: class Expr: def __init__(self, name=""):
6
6488
by: Brendan.Collins | last post by:
Hi I have a javascript problem that has been annoying me for two days now and thought that a javascript expert might have the magic solution. I am populating a table dynamically from the database and I am trying to allow the user to order the rows in the table using up and down arrows. The function I am using is:
2
2564
by: D. Dante Lorenso | last post by:
First I created a function that selected the next available pin code from a table of pre-defined pin codes: CREATE FUNCTION "public"."get_next_pin_code" () RETURNS varchar AS' DECLARE my_pin_code VARCHAR; BEGIN ... /* this is the pincode we just fetched */ RETURN (my_pin_code);
9
1628
by: Project2501a | last post by:
hey guys, Question about the internal workings of Access. How are controls added in the Form.Controls collection? I mean, in which order? the order place them on the form? is there a way to re-arrange them as i please? My solution is to remove the objects from the Form.Controls collection, place them in a tmpCollection, arrange tmpCollection as I please, and then re-add them back into the Form.Controls collection. thanks
3
1937
by: Ryan | last post by:
My project uses the /ORDER specifier to order functions as specified in file containing the decorated names of packaged functions (COMDATs). I am in the process of upgrading from VC6 to VC.NET 2003. When linking a release build of my project VC.NET 2003, the following warning occurs for each function that is being ordered: SUTL_X86OrderComdats.dat : warning LNK4065: '?SUTL_SomeFunction@@YAXXZ' cannot be ordered; ignored The help for...
3
1440
by: marc | last post by:
I want to create a strongly typed collection and use DictionaryBase I add a number of objects but when i loop through the list the data is ordered in a strange maner. How is this possible? Can anybody tell my why the order gets changed? Thanks Marc '***************** OUTPUT **************************
23
11320
by: illegal.prime | last post by:
Hi all, is there a key value collection in .Net that maintains the ordering in which I add items. I'm finding that I'm creating a number of classes that contain a name and then some object. I would prefer just to use some collection that maintains the ordering in which I add things (like ArrayList), but that maps a key to a value. I thought NameValueCollection was the right one - but it doesn't guarantee ordering.
36
2085
by: dspfun | last post by:
Hi! Is there any way in C that one can guarantee that instructions are executed in the same order as they are written in the program? For example, say I have the following: instruction1; instruction2; instruction3;
20
2463
by: Mr.SpOOn | last post by:
Hi, I need a structure to represent a set of integers. I also need to perform on this set some basic set operations, such as adding or removing elements, joining with other sets and checking for the presence of specific elements. I think that using Python sets would be the best choice, but I also need integers to be ordered inside the set and I've just found out that, instead, Python sets are unordered collections.
0
9487
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
9884
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9735
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8736
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7285
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6556
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3828
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3395
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2697
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.