L
Leonard Poon
I've made a form for data searching in database. Users can input the
criteria in the fields provided. Not all fields has to be inputted with
values. In other words, the "WHERE"-clause of queries have to be dynamically
constructed at runtime. I know this can be easily done in sproc. But, can it
also be done in a "View". The reason for asking this is because I heard that
the index will not be used for dynamic sql in sproc and it is not a good way
of writing queries as well. So, I keep this on mind and avoid using dynamic
sql in sproc.
I currently use the below style of "WHERE"-clause for my queries in order to
accept criteria from users.
SELECT *
FROM dbo.Object
WHERE ((Field1=@iField1 and @iField1 is not null) or @iField1 is null) and
((Field2=@strField2 and @strField2 is not null) or @strField2 is null) and
...
* the @iField1 parameter stores the value of a listbox in the data search
form,
* the @strField2 parameter stores the freeform text inputted by user as a
criteria
Is there any way I can rewrite my query to make it more efficient? Any
advice for me?
criteria in the fields provided. Not all fields has to be inputted with
values. In other words, the "WHERE"-clause of queries have to be dynamically
constructed at runtime. I know this can be easily done in sproc. But, can it
also be done in a "View". The reason for asking this is because I heard that
the index will not be used for dynamic sql in sproc and it is not a good way
of writing queries as well. So, I keep this on mind and avoid using dynamic
sql in sproc.
I currently use the below style of "WHERE"-clause for my queries in order to
accept criteria from users.
SELECT *
FROM dbo.Object
WHERE ((Field1=@iField1 and @iField1 is not null) or @iField1 is null) and
((Field2=@strField2 and @strField2 is not null) or @strField2 is null) and
...
* the @iField1 parameter stores the value of a listbox in the data search
form,
* the @strField2 parameter stores the freeform text inputted by user as a
criteria
Is there any way I can rewrite my query to make it more efficient? Any
advice for me?