File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes Dynamic ORDER BY in Stored Procs Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Dynamic ORDER BY in Stored Procs" Watch "Dynamic ORDER BY in Stored Procs" New topic

Dynamic ORDER BY in Stored Procs

Mike Fourier

Joined: Apr 02, 2008
Posts: 25
Apologies: This is not really a JDBC question. But I thought someone here might know.

In Sybase, I'm trying to do dynamic ORDER BY clause in my stored procedure, and the trick is, I also want to dynamically determine direction (ASC, DESC).

Using Google, I've come up with this, but I'm wondering if anyone knows a way to do this, without having to duplicate significant portions of code:

Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 33119

This is the right forum for general SQL questions and stored procedures unless we have another forum dedicated to it.

My Sybase is from a number of years ago, but I vaguely remember being able to store the SQL statement in a stored proc variable and run it using "exec". This would let you just concat the one piece that is different without duplicating the bulk of the query.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
I agree. Here's the link:
subject: Dynamic ORDER BY in Stored Procs
It's not a secret anymore!