• John has it nailed. If you put in a RECOMPILE hint, nothing gets cached because there's no need since each execution gets a new compilation.

    Why would Optimize for Ad Hoc not be applicable? Except for discussion around the RECOMPILE hint & caching, it sounds very applicable. You seem to be building a very ad hoc focused process here.

    Also, exec does not allow for parameterized queries which introduces the possibility of SQL Injection. Be sure you're avoiding that issue.

    "The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
    - Theodore Roosevelt

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning