Constructing T-SQL WHERE condition at runtime
- by Nickson
I would like to implement a search function where a user passes all the arguments to the "WHERE" clause at runtime. For example in query below,
SELECT Col1, Col2, Col3, Col4
FROM MyTable
WHERE Col2 = John
1.Now what i want is to give the user a dropdownlist of columnns such that the user selects
a column to search by at runtime
Also instead of precoding Col2 = John, i want the user to choose their own operator at
runtime(such as choosing between =, <, <, <, LIKE, IN)
i basically want to contruct a query like
SELECT Col1, Col2, Col3, Col4
FROM MyTable
WHERE (@FieldToSearchBy e.g Col3, @OperatorToUserInSearach e.g LIKE, @ValueToSearch e.g John)
I want to pass
@FieldToSearchBy,
@OperatorToUserInSearach,
@ValueToSearch) as user specified parameters at runtime
I want to do this with a TableAdpter like in this example
http://www.codeproject.com/KB/database/TableAdapter.aspx