Order By using a parameter.........

Posted on 2007-10-01
Last Modified: 2009-09-14
How can I use a parameter to pick a sort condition.  I'm trying the following and it's not working.

Thanks in advance.

order by case when :param_sort = '1' then 'r.full_name'
when :param_sort = '2' then ''
when :param_sort = '3' then 't.prname'
else 'r.unique_name'
Question by:Weller0123
    LVL 9

    Accepted Solution

    You just need to provide the parameter names, without the "bind" indicator... and don't include the quotes around the column name:

    ORDER BY (CASE WHEN param_sort='1' THEN r.full_name
                                   WHEN param_sort='2' THEN
                                   WHEN param_sort='3' THEN t.prname
                                   ELSE  r.unique_name
    LVL 23

    Assisted Solution

    Use a simple decode, see the example:

    select a,b,c,d from
     (select 5 a,2 b,3 c,4 d from dual
    union all
    select 4 a,1 b,2 c,3 d from dual
    union all
    select 3 a,0 b,1 c,2 d from dual)
    order by decode(&param_sort,1,a,2,b,3,c,d) desc
    Enter value for param_sort: 1
    old   7: order by decode(&param_sort,1,a,2,b,3,c,d) desc
    new   7: order by decode(1,1,a,2,b,3,c,d) desc

             A          B          C          D
    ---------- ---------- ---------- ----------
             5          2          3          4
             4          1          2          3
             3          0          1          2
    LVL 1

    Expert Comment

    Forced accept.

    EE Admin

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    PRTG Network Monitor: Intuitive Network Monitoring

    Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

    Suggested Solutions

    Title # Comments Views Activity
    display data from previous rows 5 57
    PL/SQL Two statements 6 33
    Oracle PL/SQL syntax 4 29
    Pl/SQL Query 31 35
    This article started out as an Experts-Exchange question, which then grew into a quick tip to go along with an IOUG presentation for the Collaborate confernce and then later grew again into a full blown article with expanded functionality and legacy…
    Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
    This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
    Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now