Hi-I scalped this script from here: [url]http://www.sql-server-performance.com/articles/per/automatic_reindexing_sql2000_p3.aspx[/url]and am having some issues with it. Here is the edited script:CREATE PROCEDURE sp_defragment_indexes @maxfrag DECIMALAS/* T.PullenThis stored procedure checks index fragmentation in a database and defragmentsindexes whose scan densities fall below a specified threshold, @magfrag, whichis passed to the SP. This SP was initially based on a code sample in SQL Server 2000Books Online.Must be run in the database to be defragmented.*/-- Declare variablesSET NOCOUNT ONDECLARE @tablename VARCHAR (128)DECLARE @execstr VARCHAR (255)DECLARE @objectid INTDECLARE @objectowner VARCHAR(255)DECLARE @indexid INTDECLARE @frag DECIMALDECLARE @indexname CHAR(255)DECLARE @dbname sysnameDECLARE @tableid INTDECLARE @tableidchar VARCHAR(255)--check this is being run in a user databaseSELECT @dbname = db_name()IF @dbname IN ('master', 'msdb', 'model', 'tempdb')BEGINPRINT 'This procedure should not be run in system databases.'RETURNEND--begin Stage 1: checking fragmentation-- Declare cursorDECLARE tables CURSOR FORSELECT convert(varchar,so.id)FROM sysobjects soJOIN sysindexes siON so.id = si.idWHERE so.type ='U'AND si.indid < 2AND si.rows > 0-- Create the temporary table to hold fragmentation informationCREATE TABLE #fraglist (ObjectName CHAR (255),ObjectId INT,IndexName CHAR (255),IndexId INT,Lvl INT,CountPages INT,CountRows INT,MinRecSize INT,MaxRecSize INT,AvgRecSize INT,ForRecCount INT,Extents INT,ExtentSwitches INT,AvgFreeBytes INT,AvgPageDensity INT,ScanDensity DECIMAL,BestCount INT,ActualCount INT,LogicalFrag DECIMAL,ExtentFrag DECIMAL)-- Open the cursorOPEN tables-- Loop through all the tables in the database running dbcc showcontig on each oneFETCH NEXTFROM tablesINTO @tableidcharWHILE @@FETCH_STATUS = 0BEGIN-- Do the showcontig of all indexes of the tableINSERT INTO #fraglistEXEC ('DBCC SHOWCONTIG (' + @tableidchar + ') WITH FAST, TABLERESULTS, ALL_INDEXES, NO_INFOMSGS')FETCH NEXTFROM tablesINTO @tableidcharEND-- Close and deallocate the cursorCLOSE tablesDEALLOCATE tables-- Report the ouput of showcontig for results checkingSELECT * FROM #fraglist-- Begin Stage 2: (defrag) declare cursor for list of indexes to be defraggedDECLARE indexes CURSOR FORSELECT ObjectName, ObjectOwner = user_name(so.uid), ObjectId, IndexName, ScanDensityFROM #fraglist fJOIN sysobjects so ON f.ObjectId=so.idWHERE ScanDensity <= @maxfragAND INDEXPROPERTY (ObjectId, IndexName, 'IndexDepth') > 0-- Write to output start time for information purposesSELECT 'Started defragmenting indexes at ' + CONVERT(VARCHAR,GETDATE())-- Open the cursorOPEN indexes-- Loop through the indexesFETCH NEXTFROM indexesINTO @tablename, @objectowner, @objectid, @indexname, @fragWHILE @@FETCH_STATUS = 0BEGINSET QUOTED_IDENTIFIER ONSELECT @execstr = 'DBCC DBREINDEX (' +RTRIM(@objectowner) + '.' + RTRIM(@tablename) + ', ' + RTRIM(@indexname) + ') WITH NO_INFOMSGS'SELECT 'Now executing: 'SELECT(@execstr)EXEC (@execstr)SET QUOTED_IDENTIFIER OFFFETCH NEXTFROM indexesINTO @tablename, @objectowner, @objectid, @indexname, @fragEND-- Close and deallocate the cursorCLOSE indexesDEALLOCATE indexes-- Report on finish time for information purposesSELECT 'Finished defragmenting indexes at ' + CONVERT(VARCHAR,GETDATE())-- Delete the temporary tableDROP TABLE #fraglistGO
*************************************************************i had to do some editing to the original script here to get the stored procedure to take:Original excerpt:SELECT @execstr = 'DBCC DBREINDEX (' + "'" +RTRIM(@objectowner) + '.' + RTRIM(@tablename) + "'" + ', ' + RTRIM(@indexname) + ') WITH NO_INFOMSGS'Edit:SELECT @execstr = 'DBCC DBREINDEX (' +RTRIM(@objectowner) + '.' + RTRIM(@tablename) + ', ' + RTRIM(@indexname) + ') WITH NO_INFOMSGS'
when i attempt to run the SP like this:use <database>goEXEC sp_defragment_indexes 95.00
I receive the generic error:quote:
Server: Msg 170, Level 15, State 1, Line 1Line 1: Incorrect syntax near '.'.Server: Msg 170, Level 15, State 1, Line 1Line 1: Incorrect syntax near '.'.Server: Msg 170, Level 15, State 1, Line 1Line 1: Incorrect syntax near '.'.Server: Msg 170, Level 15, State 1, Line 1Line 1: Incorrect syntax near '.'.Server: Msg 170, Level 15, State 1, Line 1Line 1: Incorrect syntax near '.'.