title: SP taking 15 minutes, but the same query when executed returns results in 1-2 minutes
body: <p>So basically I have this relatively long stored procedure. The basic execution flow is that it <code>SELECTS INTO</code> some data into temp tables declared with he <code>#</code> sign and then runs a cursor through these tables a generate a 'running total' into a third temp table which is created using <code>CREATE</code>. Then this resulting temp table is joined with other tables in the DB to generated the result after some grouping etc. The problem is that this SP had been running fine until now returning results in 1-2 minutes. And now suddenly its taking 12-15 minutes. If I extract the query from the SP and executed it in the management studio by manually setting the same parameters it returns results in 1-2 minutes but the SP takes very long. Any idea what could be happening. I tried to generate the Actual Execution plans of both the Query and the SP but it couldn't generate it because of the cursor. Any idea why the SP takes so long while the query doesn't?</p>