You could put this in a stored procedure with a little rewriting using dynamic SQL. Since the execution plan will have to be generated each time anyway, executing from client or server code will be the same.
Alternatively, since there are only two paths, you could write two stored procedures. This would avoid the execution plan generation on compile.
From the code above, there is something not right. The only difference appears to be the logical operator 'OR'. In path 1, the statement would filter on tblPCRDdata.Option_ID. In path 2, the statement would optionally filter, tblMain.HHSAgency = tblAgency.ID OR tblPCRDdata.Option_ID. That doesn't make sense to me. I may be missing the business logic completely.
Adam Gossage
Lake Wylie, SC, USA
|