Results 1 to 14 of 14

Thread: [RESOLVED] Allow users to build WHERE clauses

Hybrid View

  1. #1
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    5,268

    Re: Allow users to build WHERE clauses

    Do you mean like providing a TextBox the user can enter "WHERE Salary>=2000"?

    I'd rather first check, if the way i'd usually do something like this would be feasable (if i ever would get such an order):
    Think Mac-Finder-App --> kinda like an Explorer with Miller-Columns

    First column: available tables to choose from (if it's always the same table, then you don't need that one)
    Next column: available columns
    after selecting a column (move it from left ListBox to right Listbox?) offer a Dialogue/TextBox to enter the value, via Dropdown the operator ("=", "<", "LIKE" etc.), and optional the boolean operator for the next one (AND/OR/NOT)

    It's just a concept-idea of mine. No Idea if it's practicable.
    OTOH, the DMS at the company i work for provides exactly such a dialogue to the user if he's searching a document in the archive

    Additionally, you wouldn't really need to think about injection (i take it you'll use parameters anyway)
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  2. #2

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Allow users to build WHERE clauses

    Quote Originally Posted by Zvoni View Post
    Do you mean like providing a TextBox the user can enter "WHERE Salary>=2000"?
    I could offer this as an immediate soluion - novice users would fail quickly.

    I'd rather first check, if the way i'd usually do something like this would be feasable (if i ever would get such an order):
    Think Mac-Finder-App --> kinda like an Explorer with Miller-Columns

    First column: available tables to choose from (if it's always the same table, then you don't need that one)
    Next column: available columns
    after selecting a column (move it from left ListBox to right Listbox?) offer a Dialogue/TextBox to enter the value, via Dropdown the operator ("=", "<", "LIKE" etc.), and optional the boolean operator for the next one (AND/OR/NOT)
    I was headed in this direction - select the column, then enter the values. Currently when I do the client-side filtering I already have the data so the dialogue with the user is more clear (like Excel does when you filter a column with NOT SO MANY distinct values!).

    Allowing the users to enter the "operator" - that's interesting. In my current client side filtering I only allow = or NOT =...and I force AND's which gets useless if you do some NOT's - looking to enhance this area soon as well.

    Additionally, you wouldn't really need to think about injection (i take it you'll use parameters anyway)
    I'm in a STORED procedure, building a SQL select statement in code - in the SPROC. There are no parameters at this point.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  3. #3
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    5,268

    Re: Allow users to build WHERE clauses

    Quote Originally Posted by szlamany View Post
    Allowing the users to enter the "operator" - that's interesting. In my current client side filtering I only allow = or NOT =...and I force AND's which gets useless if you do some NOT's - looking to enhance this area soon as well.
    I feel you.
    I've had users trying to formulate such a WHERE-Clause running into a "deadlock" (at least i call it a deadlock --> basically the one filter excluding the other) returning an empty query.

    On a sidenote: Since you mentioned Excel (and its Autofilter-Function):
    Instead of "LIKE" you could offer "Starts with" (MyColumn LIKE 'AValue%'), "Ends with" (MyColumn LIKE '%AValue') or "Contains" (LIKE '%AValue%')
    Of course you would have to do a sanity check on the value (stripping wildcards if a user thinks he's clever and enters %AValue% in that TextBox)

    As for the boolean Operators AND/OR: Yeah

    I could never explain why a WHERE SomeValue=1 AND SomeOtherValue=2 OR SomeOtherValue=5 failed for the Users (a.k.a. returning not expected results)
    You try to teach them placing paranthesis at the correct position: WHERE SomeValue=1 AND (SomeOtherValue=2 OR SomeOtherValue=5)
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

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