Is there a way to add OPTION (RECOMPILE) in C# while executing stored procedure by System.Data.SqlClient?
What I'm looking for in my imagination would be something like
using (SqlConnection sqlConn = new SqlConnection(CONN_STR))
{
sqlConn.Open();
using (SqlCommand cmd = new SqlCommand("usp_xyz OPTION (RECOMPILE)", sqlConn))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("x", x);
cmd.ExecuteNonQuery();
}
}
Yes, you can use the
EXEC... WITH RECOMPILEsyntax, but you must do it as an ad-hoc batch, and therefore specify all parameters. You cannot use this withCommandType.StoredProcedure.If you want, you could use
sp_recompile, but this has different semantics: it does not just generate a new plan for this run, it discards the old plan for all future runs of the procedure.