Jun
20
Written by:
Steve Gray
6/20/2011 9:04 AM
I’m writing a new web site (which I hope to announce soon) and I need to use a dynamic where clause. The user is going to enter some filter conditions on the web site and the data that they see will be filtered by that criteria. I’m using the RadFilter control by Telerik; it basically gives me the complete where clause.
So, I have a something like
Select * from myTable Where @WhereClause
Of course, that’s not valid SQL. What to do? In the end, I’ll have to use my least favorite solution, dynamic SQL using the EXEC command. I’ll change the statement above into
DECLARE @sql varchar(1000)
SET @sql = ‘SELECT * FROM myTable WHERE ‘ + @WhereClause
exec(@sql)
Ug. At anyrate, the best advice that I could find on the subject was here: http://sommarskog.se/dyn-search.html, I didn’t want to lose the reference so I’ll blog it.
As always, I welcome your comments!