1

How can I recompile manually a stored procedure in SQL Server? Is just like a drop and create?

I know can do it with an option in the create statement (WITH RECOMPILE), and it will be recompiled every execution, and executing sp_recompile @procedureName, which will only recompile it the next time it executes, but how can I recompile it manually without these two approaches?

mwigdahl
  • 16,268
  • 7
  • 50
  • 64
colau
  • 31
  • 3

2 Answers2

3

Stored Procedures are compiled when they are "first" executed. "First" meaning, they have no chached plan. All you can do is invalidate the cache. In SQL-Server, there is no way to force compilation other than calling it.

(BTW, this is unlike Oracle, where you may have gotten the idea from)

In our application, we maintain a script called "run_sample_statements.sql". This contains a number of representative queries and procedure calls to cause compilation and caching. We used that after maintenance and index-rebuilds to test/precache the system before we release it for the users again.

Oliver
  • 3,225
  • 1
  • 18
  • 12
0

Well, one way to do it is to find the plan handle by querying

Select st.Plan_handle 
from 
sys.dm_exec_cached_plans 
    CROSS APPLY 
sys.dm_exec_sql_text(plan_handle) st 
WHERE text like '% your proc name %'

Then DBCC FREEPROCCACHE (plan_handle)

This method is effectively the same as sp_recompile.

Sean
  • 101
  • 5