CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Nov 2003
    Posts
    101

    Making ORDER BY parameterized

    I'm using an Access database for a backend to an ASP.NET site and was wondering how to go about setting up and storing a parameterized query that uses a parameter to specify which field to ORDER BY. I would like the parameter to be a field name. I can't seem to get it to work properly and would appreciate an example. Below is one of the queries for which I would like to be able to do this by specifiying either Members.MemberName, DKPs, or LifetimeEarned in the ORDER BY clause.

    On a similar note, if I have the following query:
    Code:
    SELECT Members.MemberName, Sum(DKPAudits.DKPAuditAdjPts) AS DKPs, Sum(IIf(DKPAudits.DKPAuditType=2,-DKPAudits.DKPAuditAdjPts,0)) AS PtsSpent, Sum(IIf(DKPAudits.DKPAuditType<>2,DKPAudits.DKPAuditAdjPts,0)) AS [Lifetime Earned], Count(IIf(DKPAudits.DKPAuditType=2,1,Null)) AS Epics
    FROM Members INNER JOIN DKPAudits ON Members.MemberID=DKPAudits.DKPAuditMemberID
    GROUP BY Members.MemberName
    ORDER BY Sum(DKPAudits.DKPAuditAdjPts) DESC;
    Why will it not accept "DKPs" in the ORDER BY clause when i already specify that Sum() AS DKPs above? Do I really need the same sum in the SELECT and ORDER BY clauses?

  2. #2
    Join Date
    Feb 2001
    Location
    Sydney, Australia
    Posts
    1,909

    Re: Making ORDER BY parameterized

    Quote Originally Posted by TSmooth
    Why will it not accept "DKPs" in the ORDER BY clause when i already specify that Sum() AS DKPs above? Do I really need the same sum in the SELECT and ORDER BY clauses?
    I'm not sure why it doesn't work for you - but it should be. Actually, the ORDER BY clause is the only part of the query where you CAN use column aliases.
    Best regards,
    Igor Sukhov

    www.sukhov.net

  3. #3
    Join Date
    Dec 2002
    Location
    Myanmar
    Posts
    84

    Re: Making ORDER BY parameterized

    so surprise!!!
    it might be the nature or the weakness of Access...
    Insteads of repeating expression, just use column number such as "order by 2"
    Let's go together
    _columbus2003_

  4. #4
    Join Date
    Feb 2001
    Location
    Sydney, Australia
    Posts
    1,909

    Re: Making ORDER BY parameterized

    Quote Originally Posted by columbus2003
    so surprise!!!
    it might be the nature or the weakness of Access...
    Insteads of repeating expression, just use column number such as "order by 2"
    "ORDER BY Column" number is a very bad practise. Person responsible for maintaining your code will curse you.
    Best regards,
    Igor Sukhov

    www.sukhov.net

  5. #5
    Join Date
    Dec 2002
    Location
    Myanmar
    Posts
    84

    Re: Making ORDER BY parameterized

    anyway, I hope you will agree it is better than repeating the expression.
    Let's go together
    _columbus2003_

  6. #6
    Join Date
    Feb 2001
    Location
    Sydney, Australia
    Posts
    1,909

    Re: Making ORDER BY parameterized

    Quote Originally Posted by columbus2003
    anyway, I hope you will agree it is better than repeating the expression.
    I totally disagree with you. It's better to repeat the already known expression
    than to use less clear syntax (BTW I've heard this syntax is deprecated).
    Last edited by Igor Soukhov; October 10th, 2005 at 06:47 AM.
    Best regards,
    Igor Sukhov

    www.sukhov.net

  7. #7
    Join Date
    Oct 2005
    Location
    India
    Posts
    24

    Lightbulb Re: Making ORDER BY parameterized

    Hi,

    For running the parameterized query you have to put the query in the query section of the MS Access.on the left side u can find the tables toolbox.below tables there is queries.go there and write the query as


    SELECT Members.MemberName, Sum(DKPAudits.DKPAuditAdjPts) AS DKPs, Sum(IIf(DKPAudits.DKPAuditType=2,-DKPAudits.DKPAuditAdjPts,0)) AS PtsSpent, Sum(IIf(DKPAudits.DKPAuditType<>2,DKPAudits.DKPAuditAdjPts,0)) AS [Lifetime Earned], Count(IIf(DKPAudits.DKPAuditType=2,1,Null)) AS Epics
    FROM Members INNER JOIN DKPAudits ON Members.MemberID=DKPAudits.DKPAuditMemberID
    GROUP BY Members.MemberName
    ORDER BY @variable;


    save this query there.You have to call this query from asp.net with the input values for that.the input must be the necessary field name u want.such as members or sum(audit) something.

    Try.All the best

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured