aspose file tools*
The moose likes JDBC 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

Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Databases » JDBC
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
internet detective

Joined: May 26, 2003
Posts: 29219

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.

[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
I agree. Here's the link:
subject: Dynamic ORDER BY in Stored Procs
Similar Threads
Date range help
Help for Dynamically changing sort by field and order by clause, in db2 procedure
Calculate number of days between two dates
Dynamically setting sort direction
Hibernate Select - Criteria with a Date