Skip to Main Content
  • Questions
  • Dynamically Sorting Using Stored Procedures...

Breadcrumb

Question and Answer

Tom Kyte

Thanks for the question, John.

Asked: June 29, 2004 - 9:21 am UTC

Last updated: July 01, 2004 - 7:45 pm UTC

Version: 9.2.0.4

Viewed 1000+ times

You Asked

First, I just wanted to say how GREAT you site is and how much it helps developers like me.

I currently work w/ .NET and query against several Oracle 9.2.x databases.

One of the problems I have is how to dynamically sort a SQL query using a stored procedure and an INPUT parameter which passes in the column to sort by.

I've read a previous question that was posted on your site about this and the solution was to use a dynamically build your SQL statement concatenating all of your statements together using a string then open your REF CURSOR for that string.

(Note: procedure is usually in a package that uses ref cursor as a type declared in the package header. Can you use a ref cursor in a generic stored procedure or must you always use a package?)
e.g.
CREATE OR REPLACE PACKAGE myPackage AS
TYPE refcur IS REF CURSOR;
END;
CREATE OR REPLACE PACKAGE BODY myPackage IS
PROCEDURE pSortTest(myRefCursor OUT myPackage.refcur, myInputParm VARCHAR2) AS
v_sql VARCHAR2(50);
BEGIN
v_sql := 'SELECT * FROM employees ORDER BY ' || myInputParm;
OPEN myRefCursor FOR v_sql;
END pSortTest;
END;

I know with SQL Server you can do a CASE statement in the ORDER BY clause and this approach is recommended by Microsoft rather than building the query dynamically as a string b/c it allows for the plan to be built.

e.g.
CREATE PROCEDURE pSortTest @SortParm VARCHAR AS
SELECT * FROM employees
ORDER BY
CASE
WHEN @SortParm = 'emp_name' THEN emp_name
.....
END
GO

Since I'm using ORACLE 9.2.x what would be the best approach to use and why?

Is there anything new in 10g that will make this any easier?

Thank you for your time and help,
John

and Tom said...

Hey -- I just wrote about this this month in Oracle magazine :)

</code> https://asktom.oracle.com/Misc/oramag/on-numbers-and-analytics.html <code>

and ctl-f forh
Order Anything


ref cursor would let you use an index to sort if needed, decode (or case) would always "sort after getting data".


Note: their case example needs to support dates, numbers, etc -- they need to go a little further with their example if that was it.

Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Response...

John, July 01, 2004 - 1:49 pm UTC

Tom,
The article you referenced/wrote was a great help and pretty much explained everything.
The only question I have about it is that you stated that you would only use a static SQL statement opened in an IF/THEN/ELSIF block if you have just a few columns. If you were conserned about performance and had 10 columns to sort by would you still use the IF/THEN/ELSIF statements or would you just use the dynamic SQL or DECODE method. Does the benefit you gain by using static SQL drop as you have more IF/THEN/ELSIF statements or does it just simply make life easier on you to by not having to type out 10 SQL statements and 10 IF/THEN/ELSIF clauses.

Thanks again for all of your help,
John

Tom Kyte
July 01, 2004 - 7:45 pm UTC

10 is getting to be more than a few. I would probably use a dynamically opened ref cursor at that point (well, i do).

the benefit of static sql is the dependency mechanism -- using dynamic sql defeats it. It is one of the powers of plsql -- that you know what procedures use what objects.

More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library