How to force SQL Server 2005 objects to be recompiled NOW
According to this running sp_recompile
forces the object to be recompiled the next time that it is run
I need it to be recompiled the moment I run the sp-recompile command, mainly to check for syntax errors and existence of objects on which the stored proce开发者_StackOverflowdure depends.
-- on sql 2008 there's sys.sp_refreshsqlmodule module...
Probably the simplest way to do this is to re-deploy the stored procedure, which would (as far as I'm aware) remove the need to recompile the procedure.
Something along these lines:
SET @ProcedureName = 'SampleProcedure'
CREATE TABLE #ProcedureContent (Text NVARCHAR(MAX))
INSERT INTO #ProcedureContent
EXEC sp_helptext @ProcedureName
DECLARE @ProcedureText NVARCHAR(MAX)
SET @ProcedureText = ''
SELECT @ProcedureText = @ProcedureText + [Text] FROM #ProcedureContent
EXEC ('DROP PROCEDURE ' + @ProcedureName);
EXEC (@ProcedureText)
DROP TABLE #ProcedureContent
精彩评论