I am using VS 2010, SQL 2008 express and doing windows applicaion programming in vb.net.I want to write a select query(or Stored Procedure) such that the conditions mentioned under WHERE clause can be variable .like :

Select * from TABLE where F1 = var1 and F2 = var2 and F3 = var3.

Now on the basis of some conditons the equations in where clause should be used.Say condition is variable value >6.
Now in this case ,if

var1 =7 var2 =3 var3 = 8 then the query shud behave like


select * from TABLE where F1 = var1 and F3 = var3

if :

var1 =3 var2 = 4 var3 =3 the :

select * from TABLE

But only one query is to be wrote that satisfies all of these


Edited by amehra: n/a

7 Years
Discussion Span
Last Post by lolafuertes

If I understood well, you need to create and ad hoc query on the VB application with a variable numer of conditions depending on some rules.

I will suggest you to create a variale where clause like in the following example:

Dim strQuery as String = "SELECT * FROM table WHERE 1=1 " ' do not foret the space after
Dim strWhere as String = ""
' here go the rules
If var1 > 6 then
    strWhere &= "AND F1 = " & var1 & " " ' again not forget the space
End If
If var2 > 6 then
    strWhere &= "AND F2 = " & var2 & " "
End If
Dim Commandtext as String = strQuery & strWhere

Hope this helps

Edited by lolafuertes: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.