Interview Question of the Week #063 – How to Recompile Stored Procedure for Specific Table?

I have kept busy with SQL Consultancy now a days. Just the other day, while doing performance tuning exercise we had made major changes in the data stored in one of the main tables. The impact was so huge that we realized that it will impact how the SQL Server engine is building execution plans for that table. This brought up a question among us that – How to Recompile Stored Procedure for Specific Table? Well, this time the question was really not asked in the interview, but I really feel that it will be nice to address it here in this series.

Question: How to Recompile All the Stored Procedure for Specific Table?

Answer: Before I give answer, I must tell you that this is not something you should run every day. SQL Server is smart enough to do this exercise when needed as well as when the services are restarted. In our case, we were very certain that it was time when it would have not impacted the performance of the server and it was safe.

Solarwinds

Here is the script which you can use to recompile all the stored procedures which are involving any specific table.

The following script will recompile all the stored procedure on table Sales.Customer in AdventureWorks database.

USE AdventureWorks;
GO
EXEC sp_recompile N'Sales.Customer';
GO

The following script will recompile specific stored procedure uspGetBillOfMaterials only.

USE AdventureWorks;
GO
EXEC sp_recompile 'uspGetBillOfMaterials';
GO

Interview Question of the Week #063 - How to Recompile Stored Procedure for Specific Table? RecompileStoredProcedure

Reference: Pinal Dave (https://blog.sqlauthority.com)

Solarwinds
, ,
Previous Post
Interview Question of the Week #062 – How to Find Table Without Clustered Index (Heap)?
Next Post
What is a Self Join? Explain with Example – Interview Question of the Week #064

Related Posts

2 Comments. Leave new

  • Will this work with dynamic SQL also?

    Reply
  • I am trying to think how would this be different from running an UPDATE STATISTICS on the table/indexes involved and let SQL Server recompile, if needed. Do you see any scenario, in which the compiler may not do this automatically in response to an update stats.

    Reply

Leave a Reply

Menu