Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Force recompile of Sql Server stored procs (See related posts)

This force a recompile of all stored procs, views, tables, triggers, and functions in the current database.
CHECKPOINT 
GO

DBCC DROPCLEANBUFFERS
GO

DBCC FREEPROCCACHE
GO

DECLARE @intDBID INTEGER 
SET @intDBID = DB_ID()

DBCC FLUSHPROCINDB (@intDBID)
GO


declare @procNames Table (procName varchar(255))
insert into @procNames
select name from sysObjects where xtype in ('V','P','U','FN','TF','TR' ) and status > 0

--select * from @procNames

set nocount off
--3. Run each command
-- =============================================
-- Declare and using a READ_ONLY cursor
-- =============================================
DECLARE RecompilableItemsCursor CURSOR
READ_ONLY
FOR select procName from @procNames

DECLARE @RecompilableItem varchar(255)
OPEN RecompilableItemsCursor


FETCH NEXT FROM RecompilableItemsCursor INTO @RecompilableItem
WHILE (@@fetch_status <> -1)
BEGIN
	IF (@@fetch_status <> -2)
	BEGIN
		DECLARE @sql varchar(300)
		select @sql = 'Exec sp_recompile ' + @RecompilableItem
		print @sql
		exec (@sql)
	END
	FETCH NEXT FROM RecompilableItemsCursor INTO @RecompilableItem
END

CLOSE RecompilableItemsCursor
DEALLOCATE RecompilableItemsCursor


GO


Comments on this post

Tramponetee posts on Aug 30, 2006 at 16:24

-- =============================================
-- Declare and using a READ_ONLY cursor
-- =============================================
DECLARE RecompilableItemsCursor CURSOR
READ_ONLY
FOR select procName from @procNames

DECLARE @RecompilableItem varchar(255)
OPEN RecompilableItemsCursor


FETCH NEXT FROM RecompilableItemsCursor INTO @RecompilableItem
WHILE (@@fetch_status = 0)
BEGIN
	
	DECLARE @sql varchar(300)
	select @sql = 'Exec sp_recompile ' + @RecompilableItem
	print @sql
	exec (@sql)

	FETCH NEXT FROM RecompilableItemsCursor INTO @RecompilableItem
END

CLOSE RecompilableItemsCursor
DEALLOCATE RecompilableItemsCursor

GO

You need to create an account or log in to post comments to this site.


Click here to browse all 5140 code snippets

Related Posts