Dynamic orderby clause for multiple columns with out Dynamic query

Hi,
        I've a query like
"select  * from tablename order by column1,column2,column3,column4,column5,column6"
in the above query the order by column will be dynamically changed. The query is placed in a stored procedures and the order by column will come by parameter.
   For ex: @orderbycol = column2,column1,column3,column4,column5,column6
                                     or
                @orderbycol = column3,column2,column1,column4,column5,coumn6
How can we manage the order by clause as dynamically without go to dynamic query.

ORDER BY CASE @sortcol1
             WHEN 'col1' THEN col1
             WHEN 'col2' THEN col2
         END,
         CASE @sortcol2
             WHEN 'col1' THEN col1
             WHEN 'col2' THEN col2
         END,
Note: these CASE expressions assumes that all columns have the same data type.
You could consider sorting in the presentation layer instead.
Erland Sommarskog, SQL Server MVP, [email protected]

Similar Messages

  • Dynamic where clause for MULTIPLE values

    In the following of dynamic where clause?
    i have some somewhat different problem.
    Is a dynamic where clause for multiple values possible?
    If the inputvariable varCode1,varCode2,varCode3,varCode4 are 0 then show all id's otherwise filter the resultset on var1 and/or var2,and/or var3,and/or var4.
    Example table:
    create table t
    (d int,var int);
    insert into t values (1 ,1 );
    insert into t values (2 ,1 );
    insert into t values (3 ,2 );
    insert into t values (4 ,3 );
    insert into t values (5 ,4 );
    insert into t values (6 ,4 );
    insert into t values (7 ,4 );
    insert into t values (8 ,4 );
    insert into t values (9 ,5 );
    insert into t values (10, 6);
    insert into t values (11, 6);
    So what i want to change the where clause upon the value of the varCodes. If var1 and var are not 0 a "AND" should be used otherwise an "OR"
    select id
    from tst
    where var = DECODE( :varCode1, 0, var, :varCode1)
    or/and(?) DECODE( :varCode2, 0, var, :varCode2)
    or DECODE( :varCode3, 0, var, :varCode3)
    or DECODE( :varCode4, 0, var, :varCode4)

    Please turn off your Caps Lock
    and try this link
    Dynamic WHERE clause

  • ADF dynamic where clause for VO query using BC

    I'm hoping someone can help me out. I have a read only view object that I want to filter results based on some user choices. For example the user may want to see all results or they may only want to see a much smaller subset. This is for an error dashboard, the smaller subset maybe by application or maybe by application and by error severity. I think I can use a dynamic where clause but I'm not sure, Can someone get me started down the right path, or post an example that I can see.

    Hi user,
    You can use a dynamic where clause for this, but maybe there is a more practical option. Maybe you can have a fixed where clause, but using a bind parameter, like:
    vo.application like :applicationParam. This makes an executeWithParams method available that you can call from your page, and use in the bindings. This parameter you can populate with the results of for example a poplist, with a default value of '%'.
    When you do want to use a dynamic where clause, you will need to add a method to the java code of your application module, and make this method available for the client. In this method, you can use findViewObject to retrieve your VO, use VO.setWhereClause to set the whereclause to what you want, and then call VO.executeQuery. This method can then again be called from the page.
    Success,
    Jeroen van Veldhuizen

  • How To Split File In to Multiple Files With out using B.P.M

    Hi Guys,
    How To Split File In to Multiple Files With out using B.P.M.
    Thanks in advance
    Regards's
    KIran.B

    Hello
    below r the links were u will find message spilitting by graphicaaly i.e without using BPM.
    /people/claus.wallacher/blog/2006/06/29/message-splitting-using-the-graphical-mapping-tool
    Sender File Adapter with file conversion  Multimapping --file content conversion with split messg mapping
    https://www.sdn.sap.com/irj/sdn/weblogs?blog=/pub/wlg/2748---- [original link is broken] [original link is broken] [original link is broken]
    multimappig
    /people/narendra.jain/blog/2005/12/30/various-multi-mappings-and-optimizing-their-implementation-in-integration-processes-bpm-in-xi
    hope this resolve your problem
    thank's
    Chetan

  • Setting different Tabel Cell SemanticColor for Multiple Columns of each row

    Hi,
    I have requirement of setting different colors to different columns for each row based on some condition in table data.
    The data to table is coming from model, hence table is mapped to model node and attributes.
    I have created Seperate Node CellColorNode with attribue CellClr1 and CellClr2 of type TextView Semantic Color.
    Set the calculated, read only attribute to True. Mapped table columns text view to the CellColorNode->CellClr1 and CellColorNode-->CellClr2 correspondingly.
    Now, my query is how do i set the colors to CellClr1 and CellClr2 attributes. As I need to set the color for multiple columns of each table row.
    Is it in method getColorCellCellClr generated? Any  Example Code?

    Its resolved by following below link
    http://scn.sap.com/thread/158286

  • Single invoice for multiple orders with different divisions

    Dear All,
    while I am trying to do single invoice for muliple orders with different divisons invoice is gettting splitted..In copy control vbrk routine I  tried with 003 and 007 etc..but result is same..what configuration need to be done for getting the single invoice
    Thanks In advance
    Regards
    CNRNAIR

    Hello
    single invoice for multiple orders with "different divisions" invoice is getting split
    FYI - Different Division means different Sales Area.
    So, sales doc (whether sales order, Delivery & Billing) is applicable to EXACTLY one sales area.
    Thus, combining docs with different sales area is practically impossible.
    Provided, you are using common division.
    Hope this assist you,
    Thanks & REgards
    JP

  • Is it possible to search for multiple folders with the same name and...

    Is it possible to search for multiple folders with the same name and then select them all and change the permissions on just those folders .i.e. Search for the budget folders in all client folders and lock them down to just the project managers. Without having to go to each folder and apply the permissions.

    user11919409 wrote:
    Is it possible to create a Clone database with the same name of source db using RMAN ...
    yes
    >
    DB version is 11.2.0.2
    Is it possible to clone a 11.2.0.2 database to 11.2.0.3 home location directly on a new server . If it starts in a upgrade mode , it is ok ....yes
    Handle:     user11919409
    Status Level:     Newbie (10)
    Registered:     Dec 7, 2009
    Total Posts:     102
    Total Questions:     28 (22 unresolved)
    why do you waste time here when you rarely get any answers to your questions?

  • Create a CR for multiple processing with me as the current processor

    Hi
    How to create a change request for multiple processing with myself as the current processor.
    I have created a change request for multiple processing and the ystem throws me the error that "There are no suitable change requests"
    Display help shows the below
    For the upload you need a change request that has the status Considered
    and Approved or To Be Revised and ideally has a type that does
    not have the property Object List Is Binding. If the
    change request does have this property, all the entities that are uploaded must
    already be assigned to this object list.
    Please can someoen guide me on how to fix this.
    I found few threads related to this but cudint understand on how to assign a status to CR
    Thanks,
    Sharma

    Hi Prateek,
    Please can anyone give any pointers on how to set the status of change request (for multiple processing):-
    For assigning the statuses to the change request:-
    Go to t code:- MDGIMG->General settings>Process Modeling>Change Request>Edit status of change request.
    the path you mentioned is to create and edit the existing Change request statuses, what I am looking for is when I create a CR for Multi record processing and try to upload a file I get the error "There are no suitable change requests". and when I check the Status of the change request I created I see that the status is Changes to be executed (Set automatically).
    how can i set myself as the processor.
    tcode: MDGIMG->General settings>Process Modeling>Change Request>Create Change request type. Ucheck the single object checkbox and assign your userid for that CR step.
    Single object is already Unchecked  and I am using BRF+ so when I am the processor First approver is say X, I am not sure if we have any place where we can set ourselves as the user. Please let me know if I missed sumting
    Thanks
    Hi Shephalika,
    Yes I am trying to upload a file and Single object is Unchecked in the Change request type.
    Thanks,
    Sharma

  • How to config a shared cache for multiple environments with C API

    How to config a shared cache for multiple environments with C API?  Just like Java edition. Chapter 2. Database Environments
    I want to open large number of databases, at least 10,000. But as the counts of databases opened increase, the db->open operation become very slow. It almost cost 2 hours to 10,000 databases.
    So I try to distribute these databases to multiple environments ( for example, 5 envs ). And in order to improve the efficient of memory use, I want to share cache between envs.

    Hi,
    It is not clear what you meaning about multiple environments. Do you mean these environments are in different directories or  in the same directory ? If you mean environments in different dirs share the same cache, it is interesting why you need that.
    If you do not use DB_PRIVATE to open the environment, the created cache will be on disk, in the environment directory, so it can be shared by multiple processes and multiple threads. Currently, the cache file is in the environment directory, and we do not support specifying a separate directory for cache only.
    Regards,
    Oracle Berkeley DB.

  • Single heading for Multiple columns in ALV

    Hi Experts,
          I need to display a common column heading for multiple columns in my ALV Grid Display output. I am giving the sample output here.
    MATNR...WERKS..| Unrestricted Stock|....|Stock in transfer        |....|Stock in Quality|.
    ............................| CAT | SAP  | Differe| .. |  CAT   |  SAP  | Diff   | .. | CAT   |  SAP  | Diff  |
    As I shown above, for 3 columns CAT, SAP, and for Difference, I need to get "Unrestricted Stock" as column heading... and so on...
    Pl. suggest me on this.
    Thanks in Advance
    Ramakrishna

    Hello Ramki,
    NOT POSSIBLE IN ALV AT ALL :-((
    You have to use classical report using WRITE stmts for this.
    If your client does not want to lose ALV functionalities, then you can try something like.
    Unrestricted Stock-SAP | Unrestricted Stock-CRM | Unrestricted Stock-Diff. Stock ... and so on !!!
    BR,
    Suhas
    Edited by: Suhas Saha on Jan 21, 2009 3:00 PM

  • How can i sign up for hbo now with out ipad

    how can i sign up for hbo now with out ipad

    You need an Apple TV or iOS device. See this link. http://www.apple.com/appletv/hbo-now/

  • How to calculate the individual sums of multiple columns in a single query

    Hello,
    Using Oracle 11gR2 on windows 7 client. I have a question on calculating sum() on multiple columns on different columns and store the results in a view. Unfortunately I could not post the problem here as it keeps on giving error "Sorry, this content is not allowed", without telling where or what it is! So I had to post it in the stack-overflow forum, here is the link: http://stackoverflow.com/questions/16529721/how-to-calculate-the-individual-sums-of-multiple-columns-in-a-single-query-ora
    Will appreciate any help or suggestion.
    Thanks

    user13667036 wrote:
    Hello,
    Using Oracle 11gR2 on windows 7 client. I have a question on calculating sum() on multiple columns on different columns and store the results in a view. Unfortunately I could not post the problem here as it keeps on giving error "Sorry, this content is not allowed", without telling where or what it is! So I had to post it in the stack-overflow forum, here is the link: http://stackoverflow.com/questions/16529721/how-to-calculate-the-individual-sums-of-multiple-columns-in-a-single-query-ora
    Will appreciate any help or suggestion.
    ThanksLooks like you want a simple group by.
    select
              yr
         ,      mnth
         ,      region
         ,     sum(handled_package)
         ,     sum(expected_missing_package)
         ,     sum(actual_missing_package)
    from test
    group by
         yr, mnth, region
    order by      
         yr, mnth, region;I wouldn't recommend storing your data for year / month in 2 columns like that unless you have a really good reason. I would store it as a date column and add a check constraint to ensure that the date is always the first of the month, then format it out as you wish to the client.
    CREATE TABLE test
         year_month                              date,
        Region                     VARCHAR2(50),
        CITY                       VARCHAR2(50),             
        Handled_Package            NUMBER,       
        Expected_Missing_Package   NUMBER,   
        Actual_Missing_Package     NUMBER
    alter table test add constraint firs_of_month check (year_month = trunc(year_month, 'mm'));
    ME_XE?Insert into TEST (year_month, REGION, CITY, HANDLED_PACKAGE, EXPECTED_MISSING_PACKAGE, ACTUAL_MISSING_PACKAGE)
      2  Values (to_date('2012-nov-12', 'yyyy-mon-dd'), 'Western', 'San Fransisco', 200, 10, 5);
    Insert into TEST (year_month, REGION, CITY, HANDLED_PACKAGE, EXPECTED_MISSING_PACKAGE, ACTUAL_MISSING_PACKAGE)
    ERROR at line 1:
    ORA-02290: check constraint (TUBBY.FIRS_OF_MONTH) violated
    Elapsed: 00:00:00.03
    ME_XE?Insert into TEST (year_month, REGION, CITY, HANDLED_PACKAGE, EXPECTED_MISSING_PACKAGE, ACTUAL_MISSING_PACKAGE)
      2  Values (to_date('2012-nov-01', 'yyyy-mon-dd'), 'Western', 'San Fransisco', 200, 10, 5);
    1 row created.
    Elapsed: 00:00:00.01
    ME_XE?select
      2        to_char(year_month, 'fmYYYY')    as year
      3     ,  to_char(year_month, 'fmMonth')   as month
      4     ,  Region
      5     ,  CITY
      6     ,  Handled_Package
      7     ,  Expected_Missing_Package
      8     ,  Actual_Missing_Package
      9  from test;
    YEAR         MONTH                REGION                         CITY                    HANDLED_PACKAGE EXPECTED_MISSING_PACKAGE ACTUAL_MISSING_PACKAGE
    2012         November             Western                        San Fransisco                       200                       10                      5
    1 row selected.
    Elapsed: 00:00:00.01
    ME_XE?Then you have nice a nice and easy validation that ensures you data integrity.
    Cheers,

  • Get Oracle SID in PL/SQL with out a query

    Hi,
    Is there any way where I can get ORACLE SID value in the PL/SQL layer with out actually querying v$database tables.
    I am looking for key word in PL/SQL which stores currect SID name, just like how keyword 'user' stores the current user who has logged in.
    Thanks
    Sree

    Hi,
    Is there any way where I can get ORACLE SID valuein
    the PL/SQL layer with out actually querying
    v$database tables.
    I am looking for key word in PL/SQL which stores
    currect SID name, just like how keyword 'user'stores
    the current user who has logged in.Subtle correction ...
    [url=http://download-east.oracle.com/docs/cd/B19306_01
    /appdev.102/b14261/subprograms.htm#sthref1776]'user'
    is a built-in function provided by Oracle. Why don't you create your own function to perform the
    Select?
    I know I can create a function but, I dont want to read the table everytime it is called. Knowing that Database sid is going to be constant for next couple of years.
    The procedure in which I am going to use database sid is called for almost every transaction in the database. I am trying to avoid IO operation for a constant value. I cant declare a global constant as it is going to be different for each environment Dev, QA, Production etc..and we are looking to deploy same code for all the environments. We are anticipating to have multiple databases for production too.

  • How can i register column with in the query

    hi all
    how can i register column with in the query?

    Thanks.
    In my business one, the column width was registed.
    But I don't know how can I do it.
    So I try to save the query again after change column width.
    Then the query was opend with changed column width.
    Is it right function?
    Is there any way to register the column width?
    It's very troublesome to change column width every time.
    seiichi

  • Sorting a Collection with dynamic columns using a custom compare function for multiple columns

    I need help and ideas on how to sort a ListCollectionView.  My problem is complicated by 3 requirements-
         1. The column values contain HTML tags that needs to be removed before sorting (use custom compareFunction to strip HTML)
         2. The columns are dynamic, so dataField names are not known at compile time (need a single compareFunction for all columns)
         3. The data is used in an AdvancedDataGrid so multi-column sorting is required
    I have figured out how to solve any 2 of the 3 requirements.  However, I am having difficulties supporting all 3 requirements.
    Any help or ideas would be greatly appreciated.  Thanks.

    After playing with this some more I think I've figured out a solution.  This seems to work in initial testing.  Also, there is not a need to capture the current sort column in the headerRelease event which many offered solutions suggested.  Another benefit to this solution is that keyboard initiated sorting is handled also.  Whereas the headerRelease event is only triggered by a mouse click on the column header and special handling is required if the user uses the keyboard to access the column header.
    One point that I don't understand is how ascending/decending order is determined.  Behavior seems to be different between a single SortField versus multiple SortFields.  Notice how the compareResults are handled for the different situations.  Anyone out there know why???
     private function colSortCompareFunction(obj1:Object, obj2:Object, fields:Array = null):int{
         var compareResults:int = 0; 
         var newObj1:Object = new Object(); 
         var newObj2:Object = new Object();
          // should not be a condition that is met   
         if (_dataProviderDetails.sort.fields == null)     {
              var s:Sort = new Sort(); 
              var f:Function = s.compareFunction; 
              return f.call(null, obj1, obj2, fields);     }
         // when a single column is selected for sorting   
         else if (_dataProviderDetails.sort.fields.length == 1)     {
              var firstFld:SortField = _dataProviderDetails.sort.fields[0];
              newObj1[firstFld.name] = stripHTML(obj1[firstFld.name]as String);          newObj2[firstFld.name] = stripHTML(obj2[firstFld.name]
    as String);
              compareResults = ObjectUtil.compare(newObj1[firstFld.name], newObj2[firstFld.name]);
               return compareResults;     }
         // when multiple columns are selected for sorting   
         else       {
              for each (var fld:SortField in _dataProviderDetails.sort.fields)          {
                   newObj1[fld.name] = stripHTML(obj1[fld.name]
    as String);               newObj2[fld.name] = stripHTML(obj2[fld.name]
    as String);
                   compareResults = ObjectUtil.compare(newObj1[fld.name], newObj2[fld.name]);
                    if (compareResults != 0)               {
                        if (fld.descending)                    {
                             return compareResults * -1;                    }
                        else                      {
                             return compareResults;                    }
               return compareResults;     }
    Does anyone see any problems with this solution?
    NOTE:  stripHTML(String) is a simple function using regular expression to remove HTML tags.
    Thx

Maybe you are looking for