Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8074905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T14:48:59+00:00 2026-06-05T14:48:59+00:00

Basically I have to build a list of items from a SQL database however

  • 0

Basically I have to build a list of items from a SQL database however the user can choose to filter on any combination of 7 filters and can also choose a column to order by and also the order by direction.

As you can imagine this ends in a large number of different combinations to code for and the dataset is quite large so I’d like to avoid doing this in the application.

In my stored procedure so far I have tried:

  • Building a query string, although this is quite simple and easy to follow, it leaves the application open to SQL injection so I would rather avoid this.

  • Using a set of IF ELSE statements to run proper parameterised SQL however this quickly becomes a huge tree and will be a nightmare to maintain.

I’m not the first person that has needed a solution like this and there must be a better way than the above? Also as a side question is there a good way to do order by and order by direction in a parameterised way, besides doing lots of IFs?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T14:49:02+00:00Added an answer on June 5, 2026 at 2:49 pm

    For filtering, use the COELSCE to filter by any combination of conditions. In your stored procedure there would be a list of all parameters that the user would search for, then in the your query will be something like:

     SELECT * 
     FROM YourTable t
     WHERE 1 = 1
     AND t.FirstColumn = COALESCE(@FirstColumnParam, t.FirstColumn)
     AND t.SecodndColumn = COALESCE(@SecondColumnParam, t.SecondColumn) 
     ....
    

    If any of the parameters is null, then the condition is skipped. and for 1=1 that’s in case there is no filter coditions is passed to your query.

    For Ordering: you can use the CASE statement to order by any choosen column:

    ORDER BY ( CASE WHEN @OrderByParam = 1 Then FirstColumn ELSE .... END)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to build a new list with a loop. Basically i can't use
I basically have a program that filters records from one excel file to another
Basically I have the following structure for a javadoc: build +---javadoc +-------Module A +-------Module
I have a kinda complex query, basically I search the database in most fields
I have a list of about 120 thousand english words (basically every word in
I am looking for a way to build a dropup menu. Basically, I have
I am trying to build a three level treeview in WPF. Basically, I have
I have a listbox that I am trying to build a triggeraction for. Basically
I have a counter of menu items, that basically add incremental value as a
I am trying to build an array from a list of URL parameters to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.