I found the code I used, I took out some stuff that was obsolete:create procedure [dbo].[sp_reindex] @row_table sql_variant=0, @size_limit bigint=0 asset nocount ondeclare @simple bit, @sql varchar(8000), @rows bigint, @max bigint, @c bigintset @max=power(2,30)if cast(sql_variant_property(@row_table, 'BaseType') as varchar) like '%char' begin set @sql=replace('dbcc dbreindex(''?'', '''', 0) with no_infomsgs;', '?', cast(@row_table as varchar(256))) exec(@sql) RETURNendset @simple=case databasepropertyex(db_name(), 'Recovery') when 'SIMPLE' then 1 else 0 endset @c=0if sql_variant_property(@row_table, 'BaseType') in ('bigint','decimal','int','numeric','smallint','tinyint') set @rows=case when @row_table is null then @max when @row_table=0 then @max else cast(@row_table as bigint) endelse set @rows=power(2,30)set @size_limit=case when @size_limit is null then @max*@max when @size_limit=0 then @max*@max else @size_limit*1024 enddeclare @tb table(tbl varchar(256), rows bigint, size bigint, i int not null identity(1,1) primary key)insert @tb(tbl, rows, size)select quotename(user_name(o.uid)) + '.' + quotename(o.name), sum(i.rows), cast(sum(i.reserved) as bigint)*8192from sysindexes i with (nolock) inner join sysobjects o with (nolock) on i.id=o.idwhere o.type='U' and i.indid<2 and i.rows<=@rowsgroup by o.uid, o.namehaving cast(sum(i.reserved) as bigint)*8192<@size_limitorder by 2 desc, 1update @tb set @c=case when @c+abs(size)>@max then abs(size) else @c+abs(size) end,size=case when @c+abs(size)>@max*2 then -abs(size) else abs(size) enddeclare z cursor fast_forward for select tbl, rows, size from @tbopen zfetch z into @sql, @rows, @size_limitwhile @@fetch_status=0 begin RAISERROR('%s - %d rows', 10, 1, @sql, @rows) with NOWAIT set @sql=replace('dbcc dbreindex(''?'', '''', 0) with no_infomsgs;', '?', @sql) if @size_limit<0 set @sql=@sql + ' checkpoint; ' + case @simple when 0 then ' exec master..MakeBackup ''' + db_name() + ''', ''L''' else '' end exec(@sql) fetch z into @sql, @rows, @size_limitendclose zdeallocate zdrop table #files
It's kinda hacky. It can reindex one table at a time, or all tables, or only those less than a certain rowcount or size in bytes. There's a line in there to do a checkpoint and a log backup, you'll have to modify or remove it to meet your needs.