set nocount on
Declare @SprocSchema varchar(128)
, @SprocName varchar(128)
Select @SprocSchema = 'dbo'
, @SprocName = 'sp_dba_Rebuild_Reorganize' --> MODIFY search criteria !! <--
Declare @set_options table
( [Option] varchar(128) not null
primary key
, [Value] int not null
)
Insert into @set_options
Select 'ANSI_PADDING' as [Option], 1 as [Value]
Union ALL Select 'Parallel Plan', 2
Union ALL Select 'FORCEPLAN', 4
Union ALL Select 'CONCAT_NULL_YIELDS_NULL', 8
Union ALL Select 'ANSI_WARNINGS', 16
Union ALL Select 'ANSI_NULLS', 32
Union ALL Select 'QUOTED_IDENTIFIER', 64
Union ALL Select 'ANSI_NULL_DFLT_ON', 128
Union ALL Select 'ANSI_NULL_DFLT_OFF', 256
Union ALL Select 'NoBrowseTable - Indicates that the plan does not use a work table to implement a FOR BROWSE operation.', 512
Union ALL Select 'TriggerOneRow - Indicates that the plan contains single row optimization for AFTER trigger delta tables.', 1024
Union ALL Select 'ResyncQuery - Indicates that the query was submitted by internal system stored procedures.', 2048
Union ALL Select 'ARITH_ABORT', 4096
Union ALL Select 'NUMERIC_ROUNDABORT', 8192
Union ALL Select 'DATEFIRST', 16384
Union ALL Select 'DATEFORMAT', 32768
Union ALL Select 'LanguageID', 65536
Union ALL Select 'UPON - Indicates that the database option PARAMETERIZATION was set to FORCED when the plan was compiled.', 131072
declare @runValue int
declare @prevrunValue int
select @runValue = -1
, @prevrunValue = -1
Declare @RunTot table
( Set_Option_Value int not null
, [Option] varchar(128) not null
, Option_value int not null
, Active int not null
, Remainder int not null
, ProcessOrder int not null
)
Insert into @RunTot
select cast(A.Value as varchar(128))
, SO.[Option]
, SO.value as Ovalue
, 0 as Active
, 0 as Remainder
, Row_number() over ( order by SO.[value] desc ) as ProcessOrder
from (
select *
from (
Select top 1
CP.plan_handle
FROM sys.dm_exec_cached_plans cp
CROSS APPLY sys.dm_exec_query_plan(cp.plan_handle) QP
WHERE QP.objectid = object_id(@SprocSchema + '.' + @SprocName)
) x
-- http://technet.microsoft.com/en-us/library/ms189472.aspx
CROSS APPLY SYS.dm_exec_plan_attributes(x.plan_handle) PA
where PA.attribute = 'set_options'
) A
cross join @set_options SO
Select top 1
@runValue = Set_Option_Value
from @RunTot
order by ProcessOrder
UPDATE T
SET --===== Running Total Thank you Jeff Moden
@prevrunValue = @runValue
, @runValue = Remainder = case when @runValue - T.Option_value < 0 then @runValue
else @runValue - T.Option_value
end
, Active = case @prevrunValue
when @runValue then 0
else 1
end
FROM @RunTot T
left join @RunTot RT1
on RT1.ProcessOrder - 1 = T.ProcessOrder
option ( maxdop 1 )
Select *
from @RunTot
order by Active desc
, ProcessOrder