Connecting Tech Pros Worldwide Forums | Help | Site Map

sp_recompile script/stored procedure

Newbie
 
Join Date: Oct 2007
Posts: 8
#1: Oct 18 '07
Folks:

I want to run sp_recompile on a weekly basis. Does anybody have a script / stored procedure which will get all the Stored Procedures and Tables from a database and do a recompile. I wrote this small script but it is giving me a error as "Incorrect syntax near 'sp_recompile'."

CREATE PROCEDURE usp_Recompile
AS

DECLARE @ObjName varchar(255)
DECLARE @Statement nvarchar(255)
DECLARE @Command varchar(255)


DECLARE ObjCursor CURSOR FOR
SELECT name FROM sys.objects
where type in ('P','U')

OPEN ObjCursor
FETCH NEXT FROM ObjCursor INTO @ObjName
WHILE @@FETCH_STATUS = 0
BEGIN
PRINT 'RECOMPILING ' + @ObjName
sp_recompile (@ObjName)
--SET @Statement = 'UPDATE STATISTICS ' + @TableName + ' WITH FULLSCAN'
--EXEC sp_executesql @Statement
FETCH NEXT FROM ObjCursor INTO @ObjName
END
CLOSE ObjCursor
DEALLOCATE ObjCursor






Thanks !!
iburyak's Avatar
Expert
 
Join Date: Nov 2006
Posts: 1,017
#2: Oct 19 '07

re: sp_recompile script/stored procedure


Change from this:
Expand|Select|Wrap|Line Numbers
  1. sp_recompile( @ObjName)
to this:
Expand|Select|Wrap|Line Numbers
  1. SET @Statement = 'sp_recompile ' + @ObjName
  2. Exec (@Statement)
Good Luck.
Reply