Hi Carp,
Thanks for your response. Very helpful! I should have thought of those options.
The answer to your question is that my app needs to be able to replace a parameter with a value, and that value could include not caring what is in the field. So, in the following
Code:
SELECT * FROM Table WHERE Field = @P1
I need to be able to replace the @P1 with a string value that the user provides. If the user types "Blah", then I need the query to say
Code:
SELECT * FROM Table WHERE Field = "Blah"
However, if the user does not provide a value, what I really need is for the query to be
with no where clause. But this is a pain to implement with a simple search/replace function. It becomes even more of a pain when you consider the other form:
Code:
SELECT * FROM Table WHERE Field IN (@P1)
In this case, @P1 could be '1' or '1,2,3' or nothing at all.
But with the info you provided, I can do this:
CSharp Code:
Code:
string command = "SELECT * FROM Table WHERE Field = @P1"
// User enters "Blah"
command.Replace("@P1", "Blah")
// User enters nothing at all
command.Replace("@P1", "Field OR Field IS NULL")
For the IN keyword, it's a little messier, but still workable:
CSharp Code:
Code:
string command = "SELECT * FROM Table WHERE Field IN @P1"
// User enters "Blah"
command.Replace("@P1", "Blah")
// User enters nothing at all
command.Replace("@P1", "(SELECT Field FROM Table) OR Field IS NULL")
So this will work fine. Thanks again!
Drew