мы сделали недавно такую процедуру.для этого он имеет флаг "HasBlobs"
ALTER PROCEDURE sp_rebuild_local_idexes
@RebuildClustered bit = 0
AS
BEGIN
DECLARE @objectid int
DECLARE @indexid int
DECLARE @schemaname nvarchar(130)
DECLARE @objectname nvarchar(130)
DECLARE @indexname nvarchar(130)
DECLARE @partitions bigint
DECLARE @frag float
DECLARE @command nvarchar(4000)
DECLARE @HasBlobs bit
DECLARE @index_type_desc nvarchar(255)
-- Conditionally select tables and indexes from the sys.dm_db_index_physical_stats function
-- and convert object and index IDs to names.
SELECT
--object_name(object_id),
object_id AS objectid,
index_id AS indexid,
avg_fragmentation_in_percent AS frag,
CASE WHEN (
SELECT st.object_id
from sys.tables st
inner join sys.columns sc
on st.object_id=sc.object_id
inner join sys.types styp
on sc.system_type_id=styp.system_type_id and sc.max_length=styp.max_length
inner join sys.schemas ss
on st.schema_id=ss.schema_id
where styp.schema_id=4
and styp.name<>'sysname'
and styp.name IN ('xml','nvarchar','varchar','image','text','ntext')
AND st.object_id = a.object_id
group by st.object_id
) IS NULL THEN 0 ELSE 1 END AS HasBlobs,
a.index_type_desc
INTO #work_to_do
FROM sys.dm_db_index_physical_stats (DB_ID(), NULL, NULL , NULL, 'LIMITED') AS a
WHERE avg_fragmentation_in_percent >= 5.0 AND index_id > 0
AND a.index_type_desc IN ('CLUSTERED INDEX','NONCLUSTERED INDEX')
ORDER BY a.index_type_desc
-- Declare the cursor for the list of partitions to be processed.
DECLARE partitions CURSOR FOR SELECT objectid,
indexid,
frag,
HasBlobs,
index_type_desc from #work_to_do
select * from #work_to_do
-- Open the cursor.
OPEN partitions
-- Loop through the partitions.
WHILE (1=1)
BEGIN
FETCH NEXT FROM partitions
INTO @objectid, @indexid, @frag,@HasBlobs,@index_type_desc
IF @@FETCH_STATUS < 0 BREAK
IF @RebuildClustered = 1 AND @index_type_desc != 'CLUSTERED INDEX'
CONTINUE;
SELECT @objectname = QUOTENAME(o.name), @schemaname = QUOTENAME(s.name)
FROM sys.objects AS o
JOIN sys.schemas as s ON s.schema_id = o.schema_id
WHERE o.object_id = @objectid
SELECT @indexname = QUOTENAME(name)
FROM sys.indexes
WHERE object_id = @objectid AND index_id = @indexid and type!=3
-- 30 is an arbitrary decision point at which to switch between reorganizing and rebuilding.
IF @frag < 30.0
SET @command = N'ALTER INDEX ' + @indexname + N' ON ' + @schemaname + N'.' + @objectname + N' REORGANIZE'
IF @frag >= 30.0
BEGIN
print @indexname+ @schemaname+@objectname
SET @command = N'ALTER INDEX ' + @indexname +
N' ON ' +
@schemaname +
N'.' + @objectname +
N' REBUILD' +
CASE WHEN @HasBlobs = 0 THEN ' WITH(ONLINE=ON)' ELSE '' END
print @command
END
EXEC (@command)
PRINT N'Executed: ' + @command
END
-- Close and deallocate the cursor.
CLOSE partitions
DEALLOCATE partitions
END