﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / Article Discussions / Article Discussions by Author / Discuss content posted by Farhan Sabzaali  / SQL 2005 Rebuild\Reorganize Indexes with Reports / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Sat, 25 May 2013 04:11:40 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: SQL 2005 Rebuild\Reorganize Indexes with Reports</title><link>http://www.sqlservercentral.com/Forums/Topic659216-1487-1.aspx</link><description>Hi,This script is great - thanks.  One small problem I've found, if the database collation uses a binary sort, I get an error stating that &amp;lt;databasename&amp;gt;.dbo.sys.Partitions could not be found, due to its case sensitivity.  It requires a simple change to sys.partitions to fix this.Thanks,Andrew</description><pubDate>Wed, 27 Jul 2011 03:39:59 GMT</pubDate><dc:creator>adb2303</dc:creator></item><item><title>RE: SQL 2005 Rebuild\Reorganize Indexes with Reports</title><link>http://www.sqlservercentral.com/Forums/Topic659216-1487-1.aspx</link><description>Hi Guys, I hope my script can helpUSE DATABASENAME;SET NOCOUNT ON;DECLARE @objectid int;DECLARE @indexid int;DECLARE @partitioncount bigint;DECLARE @schemaname nvarchar(130);DECLARE @objectname nvarchar(130);DECLARE @indexname nvarchar(130);DECLARE @partitionnum bigint;DECLARE @partitions bigint;DECLARE @frag float;DECLARE @command nvarchar(4000);DECLARE @dbid smallint;SET @dbid = DB_ID();SELECT    [object_id] AS objectid,    index_id AS indexid,    partition_number AS partitionnum,    avg_fragmentation_in_percent AS frag, page_countINTO #work_to_doFROM sys.dm_db_index_physical_stats (@dbid, NULL, NULL , NULL, N'LIMITED')WHERE avg_fragmentation_in_percent &amp;gt; 5.0 -- Allow limited fragmentation  AND index_id &amp;gt; 0 -- Ignore heaps  AND page_count &amp;gt; 10; -- Ignore small tablesDECLARE partitions CURSOR FOR SELECT objectid,indexid, partitionnum,frag FROM #work_to_do;OPEN partitions;WHILE (1=1)    BEGIN        FETCH NEXT           FROM partitions           INTO @objectid, @indexid, @partitionnum, @frag;        IF @@FETCH_STATUS &amp;lt; 0 BREAK;        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;        SELECT @partitioncount = count (*)        FROM sys.partitions        WHERE object_id = @objectid AND index_id = @indexid;        IF @frag &amp;lt; 30.0            SET @command = N'ALTER INDEX ' + @indexname + N' ON ' + @schemaname + N'.' + @objectname + N' REORGANIZE';        IF @partitioncount &amp;gt; 1            SET @command = @command + N' PARTITION=' + CAST(@partitionnum AS nvarchar(10));        EXEC (@command);        PRINT N'Executed: ' + @command;    ENDCLOSE partitions;DEALLOCATE partitions;DROP TABLE #work_to_do;GO</description><pubDate>Mon, 02 Nov 2009 16:44:38 GMT</pubDate><dc:creator>markryan003</dc:creator></item><item><title>RE: SQL 2005 Rebuild\Reorganize Indexes with Reports</title><link>http://www.sqlservercentral.com/Forums/Topic659216-1487-1.aspx</link><description>I am sorry everyone but I have moved to France working in a NGO so I have very little time these days to work on \ look at SQL - you are free to adapt \ modify this script with one request to share the updated one with the community so all can benefit from it.Thanks,Farhan</description><pubDate>Thu, 03 Sep 2009 14:22:32 GMT</pubDate><dc:creator>Farhan Sabzaali</dc:creator></item><item><title>RE: SQL 2005 Rebuild\Reorganize Indexes with Reports</title><link>http://www.sqlservercentral.com/Forums/Topic659216-1487-1.aspx</link><description>Has there been any thought about excluding snapshot databases as well from the process. I tried this with a snapshot database on a server and it stopped processing.</description><pubDate>Thu, 03 Sep 2009 08:41:42 GMT</pubDate><dc:creator>SA-1</dc:creator></item><item><title>RE: SQL 2005 Rebuild\Reorganize Indexes with Reports</title><link>http://www.sqlservercentral.com/Forums/Topic659216-1487-1.aspx</link><description>In SQL 2k5 (not 2000) you should run sp_updatestats after this job and it will update only statistics that are needed. It doesn't look like the @activeconnectionsindb variable is populated in this stored procedure. For example, I have an instance running Enterprise edition and set online= 0.... This script makes it seem like it should try and rebuild online anyways but since @activeconnectionsindb is not populated, it just rebuilds them as normal offline.</description><pubDate>Wed, 02 Sep 2009 13:12:23 GMT</pubDate><dc:creator>JJ-469859</dc:creator></item><item><title>RE: SQL 2005 Rebuild\Reorganize Indexes with Reports</title><link>http://www.sqlservercentral.com/Forums/Topic659216-1487-1.aspx</link><description>The script is awesome.  We know that a rebuild updates the statistics.  But do we need to run an update of the statistics for the indexes that get reorganized from this code?  Since the script is intelligent and only does the indexes that need it I would assume we wouldn't want to do a full statistics update on everything as well.  Has this thought come up and I missed it?Thanks,Mark</description><pubDate>Thu, 23 Jul 2009 13:56:22 GMT</pubDate><dc:creator>straitm-986199</dc:creator></item><item><title>RE: SQL 2005 Rebuild\Reorganize Indexes with Reports</title><link>http://www.sqlservercentral.com/Forums/Topic659216-1487-1.aspx</link><description>[quote][i]ALTER INDEX [MUTA_PI3] ON [prd_00rpd_hrm].[hrm].[MUTA] REBUILD 	WITH 	(ONLINE = ON, 	FILLFACTOR = 90, 	MAXDOP = 0)	PARTITION = 2;[/i][/quote]From http://technet.microsoft.com/en-us/library/ms188388.aspx, it seems you need to move the PARTITION = 2 before the index options.</description><pubDate>Tue, 21 Jul 2009 22:59:27 GMT</pubDate><dc:creator>willowbloom</dc:creator></item><item><title>RE: SQL 2005 Rebuild\Reorganize Indexes with Reports</title><link>http://www.sqlservercentral.com/Forums/Topic659216-1487-1.aspx</link><description>We use a similar script and recently partitiond a database. Now I receive a syntaxerror on this statement:[i]ALTER INDEX [MUTA_PI3] ON [prd_00rpd_hrm].[hrm].[MUTA] REBUILD 	WITH 	(ONLINE = ON, 	FILLFACTOR = 90, 	MAXDOP = 0)	PARTITION = 2;[/i][i]error: Msg 102, Level 15, State 1, Line 8Incorrect syntax near 'PARTITION'.[/i]I can't figure out what the correct syntax is. Leaving out the PARTITION statement works, leaving out the WITH statement works. I can't find an example combining the 2.Help please?</description><pubDate>Fri, 26 Jun 2009 02:20:13 GMT</pubDate><dc:creator>FreeHansje</dc:creator></item><item><title>RE: SQL 2005 Rebuild\Reorganize Indexes with Reports</title><link>http://www.sqlservercentral.com/Forums/Topic659216-1487-1.aspx</link><description>Unfortunately, this database is always online and fairly busy.  It captures our netflow statistics from all our networking hardware.I'm thinking we just need to put a check into the fetch loop to see if the table/index was deleted after the initial table/index query was done.   (similar to the check for the index being disabled).    -- Loop through the tables, indexes and partitions.    FETCH NEXT       FROM Local_Rebuildindex_Cursor       INTO @indexid, @tableid, @indexname, @objectname, @schemaname, @indextype, @partitionnum, @partitioncount, @currentdensity, @currentfrag;    WHILE @@FETCH_STATUS = 0    BEGIN;    	SET @StatusMsg = @StatusMsg + @NewLine    	[b]-- ***** Check if index/table is still available, if not then do not process it, print message.[/b]</description><pubDate>Fri, 12 Jun 2009 07:44:16 GMT</pubDate><dc:creator>jeff a</dc:creator></item><item><title>RE: SQL 2005 Rebuild\Reorganize Indexes with Reports</title><link>http://www.sqlservercentral.com/Forums/Topic659216-1487-1.aspx</link><description>Ideally, you should run these type of jobs when database is in maintenance window - if possible, try to schedule it when the application is not using the DB. If that is not possible, try running it with Online option = 0 - that should only look for existing tables. If that does not work, I may have to review code in detail to help you.</description><pubDate>Mon, 01 Jun 2009 12:17:40 GMT</pubDate><dc:creator>Farhan Sabzaali</dc:creator></item><item><title>RE: SQL 2005 Rebuild\Reorganize Indexes with Reports</title><link>http://www.sqlservercentral.com/Forums/Topic659216-1487-1.aspx</link><description>This has been working well for me for several weeks, then we recently updated a vendor product and I'm getting an error.  Looks like they updated their code to frequently create temporary tables and delete them.  It appears the rebuild procedure doesn't handle tables that have been deleted after the procedure was started:Msg 1088, Level 16, State 9, Line 1Cannot find the object "NetPerfMon.dbo.NetFlowDetail_579_1382041" because it does not exist or you do not have permissions.Any suggestions appreciated.</description><pubDate>Mon, 01 Jun 2009 12:11:10 GMT</pubDate><dc:creator>jeff a</dc:creator></item><item><title>RE: SQL 2005 Rebuild\Reorganize Indexes with Reports</title><link>http://www.sqlservercentral.com/Forums/Topic659216-1487-1.aspx</link><description>To be honest, I have not tried as I don't have access to one - if you can test it on dev or test environment, it would help.</description><pubDate>Mon, 16 Mar 2009 20:35:54 GMT</pubDate><dc:creator>Farhan Sabzaali</dc:creator></item><item><title>RE: SQL 2005 Rebuild\Reorganize Indexes with Reports</title><link>http://www.sqlservercentral.com/Forums/Topic659216-1487-1.aspx</link><description>The comments in the sp say its for SQL 2005 only, but will it run on sql 2008?</description><pubDate>Mon, 16 Mar 2009 18:19:23 GMT</pubDate><dc:creator>Paul-243651</dc:creator></item><item><title>RE: SQL 2005 Rebuild\Reorganize Indexes with Reports</title><link>http://www.sqlservercentral.com/Forums/Topic659216-1487-1.aspx</link><description>I have updated the script with code from Farhan  as well.</description><pubDate>Mon, 16 Mar 2009 09:11:11 GMT</pubDate><dc:creator>Steve Jones - SSC Editor</dc:creator></item><item><title>RE: SQL 2005 Rebuild\Reorganize Indexes with Reports</title><link>http://www.sqlservercentral.com/Forums/Topic659216-1487-1.aspx</link><description>Nice, very comprehensive, lot of work.</description><pubDate>Mon, 16 Mar 2009 08:03:43 GMT</pubDate><dc:creator>icata</dc:creator></item><item><title>RE: SQL 2005 Rebuild\Reorganize Indexes with Reports</title><link>http://www.sqlservercentral.com/Forums/Topic659216-1487-1.aspx</link><description>That worked great. Very nice job.</description><pubDate>Mon, 16 Mar 2009 07:21:41 GMT</pubDate><dc:creator>Rome-828069</dc:creator></item><item><title>RE: SQL 2005 Rebuild\Reorganize Indexes with Reports</title><link>http://www.sqlservercentral.com/Forums/Topic659216-1487-1.aspx</link><description>It seems the script that got published has space unicode characters in it - I am attaching a zip file with compiled query - let me know if that works for you.Thanks,Farhan Sabzaali</description><pubDate>Mon, 16 Mar 2009 06:54:40 GMT</pubDate><dc:creator>Farhan Sabzaali</dc:creator></item><item><title>RE: SQL 2005 Rebuild\Reorganize Indexes with Reports</title><link>http://www.sqlservercentral.com/Forums/Topic659216-1487-1.aspx</link><description>Maybe I am missing a step but this proc doesn't compile.Msg 102, Level 15, State 1, Line 3Incorrect syntax near ' '.Msg 102, Level 15, State 1, Line 57Incorrect syntax near ' '.Msg 102, Level 15, State 1, Line 146....edited out numerous errors.</description><pubDate>Mon, 16 Mar 2009 06:11:33 GMT</pubDate><dc:creator>Rome-828069</dc:creator></item><item><title>SQL 2005 Rebuild\Reorganize Indexes with Reports</title><link>http://www.sqlservercentral.com/Forums/Topic659216-1487-1.aspx</link><description>Comments posted to this topic are about the item [B]&lt;A HREF="/scripts/Rebuild+Index/65933/"&gt;SQL 2005 Rebuild\Reorganize Indexes with Reports&lt;/A&gt;[/B]</description><pubDate>Wed, 18 Feb 2009 03:45:13 GMT</pubDate><dc:creator>Farhan Sabzaali</dc:creator></item></channel></rss>