June 22, 2009

Recompiling Stored Procedures in SQL Server – All, Few or One at a time




Amongst other uses, recompiling your stored procedures can be useful especially when you want to ensure that you haven't deleted/renamed any columns/tables.

The following queries shown below recompile all stored procedures the next time they are run.

Recompiling all the Stored Procedures in a Database

-- Recompile all Stored Procedures and Triggers on a Database
USE AdventureWorks;
GO
EXEC sp_MSforeachtable @command1="EXEC sp_recompile '?'";
GO

Note: See my post over here for other uses of sp_MSforeachtable 8 Common Uses of the undocumented Stored Procedure sp_MSforeachtable

Recompile all the Stored Procedures in a Table

-- Recompile all Stored Procedures that act on the Customer table
USE AdventureWorks;
GO
EXEC sp_recompile N'Sales.Customer';
GO

Recompile a specific Stored Procedure

-- Recompile a specific Stored Procedure uspGetEmployeeManagers
USE AdventureWorks;
GO
EXEC sp_recompile 'uspGetEmployeeManagers';
GO


Did you like this post?
kick it on DotNetKicks.com
subscribe via rss subscribe via e-mail
print this post follow me on twitter


About The Author

Suprotim Agarwal, ASP.NET Architecture MVP works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He has also written an EBook 51 Recipes using jQuery with ASP.NET Controls.

Follow him on twitter @suprotimagarwal

 
  Feedback:

comments

1 Response to "Recompiling Stored Procedures in SQL Server – All, Few or One at a time"
  1. Sumit said...
    June 21, 2010 at 1:50 PM

    Not sure how this works, but when I ran the command it gave me
    Object '[dbo].[MY_TABLE_NAME]' was successfully marked for recompilation.

    However I made changes to the table to make sure it's SP dependencies break, it didn't protest. Gave the same error message. Not sure what's going on. Trying out the following now

    http://www.codeproject.com/KB/database/validatingsql.aspx

 

Copyright © 2009-2014 All Rights Reserved for SQLServerCurry.com by Suprotim Agarwal | Terms and Conditions