Results 1 to 11 of 11

Thread: Creating a SQL Statement with multiple WHERE conditions

  1. #1

    Thread Starter
    Addicted Member Kamienios's Avatar
    Join Date
    Dec 2009
    Location
    Warsaw, Poland
    Posts
    159

    Creating a SQL Statement with multiple WHERE conditions

    Hi There.

    I am trying to write a Function which will return a data table, filled with a data from a database.

    The Function will accept 6 diffrenet parameters as Optional. Each one of them will be an additional condition to be added to a where clause, if it`s provided when the function is beeing called.

    Is there any method in .NET to simply add a new condition to the where clause?

    Or Should I do it by working on a string?

    Thanks.

  2. #2
    Addicted Member spyk3's Avatar
    Join Date
    Apr 2009
    Location
    Dothan, AL
    Posts
    218

    Re: Creating a SQL Statement with multiple WHERE conditions

    Maybe somthing like this:
    vb Code:
    1. Public Function where(ByVal cols As String, ByVal equals As String)
    2.         Dim whereStrin As String = ""
    3.         Try
    4.             columns = cols
    5.             values = equals
    6.             whereString = "WHERE (" & build.whereColsAndVals(columns, values) & ")"
    7.         Catch ex As Exception
    8.  
    9.         End Try
    10.         Return whereString
    11.     End Function
    just add that to your selection string

  3. #3

    Thread Starter
    Addicted Member Kamienios's Avatar
    Join Date
    Dec 2009
    Location
    Warsaw, Poland
    Posts
    159

    Re: Creating a SQL Statement with multiple WHERE conditions

    To use this I must import some libraries.

    What libraries should I import?

  4. #4
    Addicted Member spyk3's Avatar
    Join Date
    Apr 2009
    Location
    Dothan, AL
    Posts
    218

    Re: Creating a SQL Statement with multiple WHERE conditions

    Sorry i borrowed that from other code i wrote that has a custom buildstring class

    here is the function in that class that is being refered to
    vb Code:
    1. Public Function whereColsAndVals(ByVal columns As String, ByVal values As String) As String
    2.         Dim retvalues As String = ""
    3.  
    4.         If columns.Contains(" ") AndAlso values.Contains(",") Then
    5.             columns = columns.Replace(" ", "")
    6.         ElseIf values.Contains(" ") Then
    7.             columns = columns.Replace(" ", ",")
    8.         End If
    9.  
    10.         If values.Contains(" ") AndAlso values.Contains(",") Then
    11.             values = values.Replace(" ", "")
    12.         ElseIf values.Contains(" ") Then
    13.             values = values.Replace(" ", ",")
    14.         End If
    15.  
    16.         Try
    17.             Dim araColumn() As String = Split(columns, ",")
    18.             Dim aravalues() As String = Split(values, ",")
    19.             Dim araRet() As String = Split(columns, ",")
    20.  
    21.             If araColumn.Count = 1 AndAlso aravalues.Count = 1 Then
    22.                 retvalues = columns & " = '" & values & "'"
    23.             Else
    24.                 Dim i As Integer = 0
    25.                 While i < araColumn.Count
    26.                     araRet(i) = araColumn(i) & " = '" & aravalues(i) & "'"
    27.                     i = i + 1
    28.                 End While
    29.                 retvalues = Join(aravalues, "), (")
    30.             End If
    31.         Catch ex As Exception
    32.             MsgBox(ex.Message)
    33.         End Try
    34.         Return retvalues
    35.     End Function
    Then just change the line from previous code:
    vb Code:
    1. Change line = whereString = "WHERE (" & build.whereColsAndVals(columns, values) & ")"
    2. Change too = whereString = "WHERE (" & whereColsAndVals(columns, values) & ")"

    Wrote it a long time ago but whatever

  5. #5
    Addicted Member spyk3's Avatar
    Join Date
    Apr 2009
    Location
    Dothan, AL
    Posts
    218

    Re: Creating a SQL Statement with multiple WHERE conditions

    Then u can make your select statement somthing like
    vb Code:
    1. selectStatement = "SELECT * FROM Database " &  where(columnValues, equalValues
    2.  
    3. or you could custumize ever part of the string to have its own string manipulation and have a final result like so:
    4. selCommand = selectString & fromString & whereString

  6. #6
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Creating a SQL Statement with multiple WHERE conditions

    *sigh*

    How about parameters?

    the SQL looks like this:
    Code:
    SELECT your_fields_go_here 
    FROM _your_table_
    WHERE 
      ((Field1 = @Parameter1) or (@Parameter1 IS NULL))
      AND ((Field2 = @Parameter2) or (@Parameter2 IS NULL))
      AND ((Field3 = @Parameter3) or (@Parameter3 IS NULL))
      AND ((Field4 = @Parameter4) or (@Parameter4 IS NULL))
      AND ((Field5 = @Parameter5) or (@Parameter5 IS NULL))
      AND ((Field6 = @Parameter6) or (@Parameter6 IS NULL))
    Then you can add your parameters. If it isn't set, then simply pass in NULL as its value, otherwise pass in the value of the variable.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  7. #7
    Addicted Member
    Join Date
    Oct 2008
    Posts
    152

    Re: Creating a SQL Statement with multiple WHERE conditions

    Parameters are definitely a better choice then concatenating your query strings. However searching for null values in a where clause loses the optimazation that table indexes provide. I would suggest creating several Stored Procedures each defining exactly the query you want to use and then just calling the appropiate stored procedure.

  8. #8
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: Creating a SQL Statement with multiple WHERE conditions

    Quote Originally Posted by Tom.Net View Post
    Parameters are definitely a better choice then concatenating your query strings. However searching for null values in a where clause loses the optimazation that table indexes provide. I would suggest creating several Stored Procedures each defining exactly the query you want to use and then just calling the appropiate stored procedure.
    Stored Procedures are not necessarily a good idea - for a start they aren't supported at all in some database providers, and even if they are supported that ties you to a specific database provider so you can't easily change databases in the future.

  9. #9
    Addicted Member
    Join Date
    Oct 2008
    Posts
    152

    Re: Creating a SQL Statement with multiple WHERE conditions

    I simply have to disagree. Of course it cant be used if type of database provider he is using doesnt allow it but there are many benefits to using them if he can. Specially compared to attempting to pad parameters full of null values and searching for nulls.

  10. #10
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: Creating a SQL Statement with multiple WHERE conditions

    Quote Originally Posted by Tom.Net View Post
    I simply have to disagree. Of course it cant be used if type of database provider he is using doesnt allow it but there are many benefits to using them if he can. Specially compared to attempting to pad parameters full of null values and searching for nulls.
    I said "not necessarily a good idea", not "never a good idea".

  11. #11
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Creating a SQL Statement with multiple WHERE conditions

    Quote Originally Posted by Tom.Net View Post
    Parameters are definitely a better choice then concatenating your query strings. However searching for null values in a where clause loses the optimazation that table indexes provide. I would suggest creating several Stored Procedures each defining exactly the query you want to use and then just calling the appropiate stored procedure.
    exqueeze me? How is the solution I provided ineeficient? All it's doing is checkign null values OF THE PARAMETERS... not the table data. Plus if there are 6 possible parameters... that's..... quite a few permutations... are you seriously suggesting writing a query for each and every possible combination? And then you'd have to write squirrely code to figure out which one to call.... how is that any better? That's nuts is what it is.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

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