if object_id(‘tempdb..#t_remove_expired_plan‘) is not null
drop table #t_remove_expired_plan
GO
create table #t_remove_expired_plan
(
id int identity(1,1),
plan_handle varbinary(500)
)
GO
insert into #t_remove_expired_plan (plan_handle)
select qs.plan_handle
from sys.dm_exec_query_stats qs
where creation_time< dateadd(hh,-24,getdate())
GO
declare @exists_data bit = 1
declare @v_plan_handle varbinary(500)
declare @str_sql varchar(1000)
while @exists_data = 1
begin
select top 1 @v_plan_handle = plan_handle from #t_remove_expired_plan
if(@v_plan_handle is not null)
begin
execute sp_executesql N‘DBCC FREEPROCCACHE(@plan_handle)‘ ,N‘@plan_handle varbinary(500)‘,@plan_handle = @v_plan_handle
end
delete top (1) from #t_remove_expired_plan
if exists(select 1 from #t_remove_expired_plan)
begin
set @exists_data = 1
end
else
begin
set @exists_data = 0
end
end
参考:https://www.cnblogs.com/quanweiru/p/5577421.html
[转][Dapper]参数化查询慢