Knowing what to do precisely is impossible, as I don't know your situation, but it
sounds like your SQL is just too complicated. In general the simplest thing you can do is to extract select subqueries into views break down parts of your SQL into other stored procedures. Another thing you can do is to write a user defined function, the parameter checking seems a prime candidate for this. You can create the user defined functions in c#. I've never actually done this, but it seems simple enough (famous last words!), see
this blog entry[
^] for guidance.
Hopefully other CPians will chip in with other advice, but this is where I'd start