473,672 Members | 2,484 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Parameterized order by clause: doesn't work

Can someone tell me why SQL seems to ignore my order by clause?
I tried to run through the debugger, but the debugger stops at the
select statement line and then returns the result set; so, I have no
idea how it is evaluating the order by clause.
THANK YOU!

CREATE proc sprAllBooks

@SortAscend varchar(4),
@SortColumn varchar(10)

as

If @SortAscend = 'DESC'
Select titles.title_id , title, au_lname, au_fname,
Convert(varchar (12), pubdate, 101) as PubDate

from authors
inner join
titleauthor
on
authors.au_id = titleauthor.au_ id
inner join
titles
on
titleauthor.tit le_id = Titles.title_id

ORDER BY au_lname
CASE @SortColumn WHEN 'title' THEN title END,
CASE @SortColumn WHEN 'au_lname' THEN au_lname END,
CASE @SortColumn WHEN 'PubDate' THEN PubDate END
DESC

ELSE

Select titles.title_id , title, au_lname, au_fname,
Convert(varchar (12), pubdate, 101) as PubDate

from authors

inner join
titleauthor
on
authors.au_id = titleauthor.au_ id
inner join
titles
on
titleauthor.tit le_id = Titles.title_id

ORDER BY
CASE @SortColumn WHEN 'title' THEN title END,
CASE @SortColumn WHEN 'au_lname' THEN au_lname END,
CASE @SortColumn WHEN 'PubDate' THEN PubDate END
GO

Jul 23 '05 #1
7 2744
On 25 Feb 2005 08:39:07 -0800, JJ****@hotmail. com wrote:
Can someone tell me why SQL seems to ignore my order by clause?
I tried to run through the debugger, but the debugger stops at the
select statement line and then returns the result set; so, I have no
idea how it is evaluating the order by clause.
THANK YOU!
Hi JJ,

You forgot to tell us how you call the procedure (what values for
@SortAscend and @SortColumn you use) and what results you get.

I tried your code and after fixing a syntax error, it works as I would
expect it to. You apparently expected something else, but what you
expected is not clear from your post.

Some small pointers to what might be your problem:
If @SortAscend = 'DESC' (snip)ORDER BY au_lname
CASE @SortColumn WHEN 'title' THEN title END,
CASE @SortColumn WHEN 'au_lname' THEN au_lname END,
CASE @SortColumn WHEN 'PubDate' THEN PubDate END
DESC


This resulted in an error. You either have to append a comma after ORDER
BY au_lname, or remove au_lname. I figured the latter is what you want,
so that's how I tested it.

The last CASE expression won't refer to the formatted pubdate, but to
the pubdate column in the table. Even though the ORDER BY does allow you
to refer to an alias from the SELECT clause, it does not allow you to do
so in an expression.

The DESC will only apply to the ordering by pubdate. The title and
au_lname are in seperate expressions that require a seperate DESC:
ORDER BY
CASE @SortColumn WHEN 'title' THEN title END DESC,
CASE @SortColumn WHEN 'au_lname' THEN au_lname END DESC,
CASE @SortColumn WHEN 'PubDate' THEN pubdate END DESC
Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 23 '05 #2
Thank you Hugo. Good points. For debugging purposes, I've been calling
the sproc from the debugger with the parameters @SortAscend = 'DESC'
and @SortColumn = 'title. The result set is not sorted by title desc,
as I would expect it should have been. Quite inexplicably (to me), the
result set now seems to be sorting by au_lname desc (!)

Note that the order by clause should only feature the case evaluation
statement (au_lname was there as a left-over from a debugging
attempt...):

ORDER BY
CASE @SortColumn WHEN 'title' THEN title END,
CASE @SortColumn WHEN 'au_lname' THEN au_lname END,
CASE @SortColumn WHEN 'PubDate' THEN PubDate END

I think I see what you are getting at about the PubDate (alias) and
pubdate (table column) name and will give that a try...

Also, you related that one part of the code resulted in an error...can
you tell me how to unearth that? The Sql debugger seemed to be silent
on that point to me...

Thank you again...

J.


Hugo Kornelis wrote:
On 25 Feb 2005 08:39:07 -0800, JJ****@hotmail. com wrote:
Can someone tell me why SQL seems to ignore my order by clause?
I tried to run through the debugger, but the debugger stops at the
select statement line and then returns the result set; so, I have no
idea how it is evaluating the order by clause.
THANK YOU!
Hi JJ,

You forgot to tell us how you call the procedure (what values for
@SortAscend and @SortColumn you use) and what results you get.

I tried your code and after fixing a syntax error, it works as I

would expect it to. You apparently expected something else, but what you
expected is not clear from your post.

Some small pointers to what might be your problem:
If @SortAscend = 'DESC' (snip)
ORDER BY au_lname
CASE @SortColumn WHEN 'title' THEN title END,
CASE @SortColumn WHEN 'au_lname' THEN au_lname END,
CASE @SortColumn WHEN 'PubDate' THEN PubDate END
DESC


This resulted in an error. You either have to append a comma after

ORDER BY au_lname, or remove au_lname. I figured the latter is what you want, so that's how I tested it.

The last CASE expression won't refer to the formatted pubdate, but to
the pubdate column in the table. Even though the ORDER BY does allow you to refer to an alias from the SELECT clause, it does not allow you to do so in an expression.

The DESC will only apply to the ordering by pubdate. The title and
au_lname are in seperate expressions that require a seperate DESC:
ORDER BY
CASE @SortColumn WHEN 'title' THEN title END DESC,
CASE @SortColumn WHEN 'au_lname' THEN au_lname END DESC,
CASE @SortColumn WHEN 'PubDate' THEN pubdate END DESC
Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)


Jul 23 '05 #3
I figured it out. The order by clause has to look like this:
ORDER BY
CASE @SortColumn WHEN 'title' THEN title END DESC,
CASE @SortColumn WHEN 'au_lname' THEN au_lname END DESC,
CASE @SortColumn WHEN 'PubDate' THEN pubdate END DESC

I put the 'DESC' parameter inside each of the case statements and now
my result set is sorting!
JJ****@hotmail. com wrote:
Thank you Hugo. Good points. For debugging purposes, I've been calling the sproc from the debugger with the parameters @SortAscend = 'DESC'
and @SortColumn = 'title. The result set is not sorted by title desc,
as I would expect it should have been. Quite inexplicably (to me), the result set now seems to be sorting by au_lname desc (!)

Note that the order by clause should only feature the case evaluation
statement (au_lname was there as a left-over from a debugging
attempt...):

ORDER BY
CASE @SortColumn WHEN 'title' THEN title END,
CASE @SortColumn WHEN 'au_lname' THEN au_lname END,
CASE @SortColumn WHEN 'PubDate' THEN PubDate END

I think I see what you are getting at about the PubDate (alias) and
pubdate (table column) name and will give that a try...

Also, you related that one part of the code resulted in an error...can you tell me how to unearth that? The Sql debugger seemed to be silent
on that point to me...

Thank you again...

J.


Hugo Kornelis wrote:
On 25 Feb 2005 08:39:07 -0800, JJ****@hotmail. com wrote:
Can someone tell me why SQL seems to ignore my order by clause?
I tried to run through the debugger, but the debugger stops at the
select statement line and then returns the result set; so, I have noidea how it is evaluating the order by clause.
THANK YOU!
Hi JJ,

You forgot to tell us how you call the procedure (what values for
@SortAscend and @SortColumn you use) and what results you get.

I tried your code and after fixing a syntax error, it works as I

would
expect it to. You apparently expected something else, but what you
expected is not clear from your post.

Some small pointers to what might be your problem:
If @SortAscend = 'DESC'

(snip)
ORDER BY au_lname
CASE @SortColumn WHEN 'title' THEN title END,
CASE @SortColumn WHEN 'au_lname' THEN au_lname END,
CASE @SortColumn WHEN 'PubDate' THEN PubDate END
DESC


This resulted in an error. You either have to append a comma after

ORDER
BY au_lname, or remove au_lname. I figured the latter is what you

want,
so that's how I tested it.

The last CASE expression won't refer to the formatted pubdate, but to the pubdate column in the table. Even though the ORDER BY does

allow you
to refer to an alias from the SELECT clause, it does not allow you
to do
so in an expression.

The DESC will only apply to the ordering by pubdate. The title and
au_lname are in seperate expressions that require a seperate DESC:
ORDER BY
CASE @SortColumn WHEN 'title' THEN title END DESC,
CASE @SortColumn WHEN 'au_lname' THEN au_lname END DESC,
CASE @SortColumn WHEN 'PubDate' THEN pubdate END DESC
Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)


Jul 23 '05 #4
On 25 Feb 2005 09:15:39 -0800, JJ****@hotmail. com wrote:
Thank you Hugo. Good points. For debugging purposes, I've been calling
the sproc from the debugger with the parameters @SortAscend = 'DESC'
and @SortColumn = 'title. The result set is not sorted by title desc,
as I would expect it should have been. Quite inexplicably (to me), the
result set now seems to be sorting by au_lname desc (!)

Note that the order by clause should only feature the case evaluation
statement (au_lname was there as a left-over from a debugging
attempt...):
Hi J,

I was unable to reproduce this. If I run the query you posted here
(after removing the left-over au_lname) with argument DESC and title, I
get the results in ascending order of title. Not sorted by au_lname.

The only way to get it to sort by descending au_lname is to leave the
left-over au_lname in and remove or comment the three CASE expressions.

Also, you related that one part of the code resulted in an error...can
you tell me how to unearth that? The Sql debugger seemed to be silent
on that point to me...


I'm talking about the code as you posted it here in your original
message (with the left-over au_lname included). I get an error when I
try to create the procedure, or when I try to run that statements by
themselves. The only ways tol solve it are to remove au_lname, to add a
comma at the end of the line or to remove the three case expressions.

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 23 '05 #5
Thanks very much again Hugo. All is well now -- with the DESC keywords
within each CASE statement:

ORDER BY
CASE @SortColumn WHEN 'title' THEN title END DESC,
CASE @SortColumn WHEN 'au_lname' THEN au_lname END DESC,
CASE @SortColumn WHEN 'PubDate' THEN pubdate END DESC

I am just starting to use the debugger in SQL and therefore am *very*
interested in learning as much as I can about using it to advantage.
Again, it didn't "complain" about:

ORDER BY
CASE @SortColumn WHEN 'title' THEN title END,
CASE @SortColumn WHEN 'au_lname' THEN au_lname END ,
CASE @SortColumn WHEN 'PubDate' THEN pubdate END
DESC

but, nor did the stored procedure return the intended result set!
I have used QA and the Profiler exclusively to help debug problems.

Jules

Jul 23 '05 #6
On 25 Feb 2005 13:00:22 -0800, JJ****@hotmail. com wrote:

(snip)
Again, it didn't "complain" about:

ORDER BY
CASE @SortColumn WHEN 'title' THEN title END,
CASE @SortColumn WHEN 'au_lname' THEN au_lname END ,
CASE @SortColumn WHEN 'PubDate' THEN pubdate END
DESC

but, nor did the stored procedure return the intended result set!
Hi Jules,

It should not complain about this - it's valid T-SQL syntax. It
specifies that the results should be ordered by three columns:
* first by (depending on the value of @SortColumn) either title or NULL,
ascending (since no direction is specified);
* next by (depending on the value of @SortColumn) either au_lname or
NULL, again ascending;
* and finally, if the previous two didn't suffice to define the sort
order, by (depending on @SortColumn) either pubdate or NULL, but for
this column a descending sort is specified.

I have used QA and the Profiler exclusively to help debug problems.


Those tools are the best (IMO), both for writing and debugging SQL code.

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 23 '05 #7
hmm...I understand what you are saying...intere sting
it makes sense now - thanks!

Hugo Kornelis wrote:
On 25 Feb 2005 13:00:22 -0800, JJ****@hotmail. com wrote:

(snip)
Again, it didn't "complain" about:

ORDER BY
CASE @SortColumn WHEN 'title' THEN title END,
CASE @SortColumn WHEN 'au_lname' THEN au_lname END ,
CASE @SortColumn WHEN 'PubDate' THEN pubdate END
DESC

but, nor did the stored procedure return the intended result set!
Hi Jules,

It should not complain about this - it's valid T-SQL syntax. It
specifies that the results should be ordered by three columns:
* first by (depending on the value of @SortColumn) either title or

NULL, ascending (since no direction is specified);
* next by (depending on the value of @SortColumn) either au_lname or
NULL, again ascending;
* and finally, if the previous two didn't suffice to define the sort
order, by (depending on @SortColumn) either pubdate or NULL, but for
this column a descending sort is specified.

I have used QA and the Profiler exclusively to help debug problems.
Those tools are the best (IMO), both for writing and debugging SQL

code.
Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)


Jul 23 '05 #8

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

Similar topics

15
2799
by: Peroq | last post by:
Hi all I'm not sure ASP is the problem, but my SQL statement seems fine to me. This works fine : strSQL = "SELECT .* FROM _RechPat INNER JOIN NivPatri ON .cod_niv = NivPatri.cod_niv WHERE NivPatri.position=2" set rs = CreateRecordset(strSQL) This doesn't work :
1
13574
by: Scott | last post by:
I have a view that contains the following SQL1 and it takes about 500ms to run with Explain Plan EXP1. However, if I add an order by clause(e.g. select * from view1 order by ID desc), it will then take about 3 mins to return. The explain plan for the query with the order by clause is as follows (EXP2). The explain plan 2 shows that there is no changes to the index selection except adding the "Sort Order by" step. The total number of...
1
6065
by: gary b | last post by:
Hello When I use a PreparedStatement (in jdbc) with the following query: SELECT store_groups_id FROM store_groups WHERE store_groups_id IS NOT NULL AND type = ? ORDER BY group_name
761
28579
by: Neo-LISPer | last post by:
Hey Recently, I researched using C++ for game programming and here is what I found: C++ game developers spend a lot of their time debugging corrupted memory. Few, if any, compilers offer completely safe modes. Unsurprisingly, there is a very high failure rate among projects using C++ for modern game development.
8
12922
by: deko | last post by:
I'm trying to open a Recordset based on a parameterized query. I'm kind of new to parameterized queries, so I'm sure I'm missing something simple. Set qdfs = db.QueryDefs Set qdf = qdfs("qryInvoices") qdf.Parameters("prmInv") = strInvoice qdf.Parameters("prmCid") = lngCustomerID Set rst = db.OpenRecordset("qryInvoices")
13
4438
by: bevanward | last post by:
Hi All I am finding unexpected results when inserted into a newly created table that has a field of datatype int identity (1,1). Basically the order I sort on when inserting into the table is not reflected in the order of the values from the identity field. Have I been wrong in assuming that it should reflect the order from the sort?
2
8410
by: rdemyan via AccessMonster.com | last post by:
I can't seem to get the following SQL statement with the ORDER BY clause to work. It does work if I order by any of the fields in the SELECT part of the statement but not if it is the field in the TRANSFORM part. The posted code is from the Immediate Pane: TRANSFORM Format(Sum(.ELEC_EUI),'#,###.00') AS EUI SELECT BUILDING_TYPE As Type, .BUILDING_TYPE_DESC As Description FROM GROUP BY .BUILDING_TYPE, . BUILDING_TYPE_DESC ORDER BY...
8
8046
by: Roland Hall | last post by:
In Access you use "*" + + "*", + can be replaced with & Calling a parameterized query in Access requires % be used in place of *, however, all that I have read show dynamic SQL passed to Access: WHERE LIKE '" & ASPvar & "' % ORDER BY ... However, my call is similar to: conn.qMyLookup strVar, rs
2
1936
by: Paul Elliott | last post by:
How are parameterized translation strings commonly handled? Suppose I need to create a string like: "file %1 failed to open", where %1 exists at runtime, but I need to create it in a way that can be internationalized. If I do something like this str = _("file ") + filename + _(" failed to open");
0
8411
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8686
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
7449
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...
0
5710
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();...
0
4230
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4424
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2821
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
2
2071
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1821
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.