Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
cubehead
Starting Member
4 Posts |
Posted - 2012-12-02 : 10:46:14
|
Hey all,I'm using the script below to re-org or rebuild indexes in all my SQL 2005 production databases. Everything works great until I tried to run it on a server which has a db that has a dash "-" in its name, then it craps out with a "Could not locate entry in sysdatabases for database 'partial_DB_name_before_the_dash'. No entry found with that name. Make sure that the name is entered correctly." I have been trying different things but none works so far. Any ideas on how to fix this?Thanks very much.DECLARE @DBName NVARCHAR(255),@TableName NVARCHAR(255),@SchemaName NVARCHAR(255),@IndexName NVARCHAR(255),@PctFrag DECIMALDECLARE @Defrag NVARCHAR(MAX)CREATE TABLE #Frag(DBName NVARCHAR(255),TableName NVARCHAR(255),SchemaName NVARCHAR(255),IndexName NVARCHAR(255),AvgFragment DECIMAL)EXEC sp_msforeachdb 'Use ?;INSERT INTO #Frag (DBName,TableName,SchemaName,IndexName,AvgFragment)Select db_name() ,object_name(s.object_id) As ObjectName ,object_name(s.object_id) As SchemaName ,i.Name As IndexName ,s.avg_fragmentation_in_percent From sys.dm_db_index_physical_stats(db_id(), Null, Null, Null, ''Sampled'') s Join sys.indexes i On i.object_id = s.object_id And i.index_id = s.index_id Where i.index_id > 0 And i.index_id < 255 And s.avg_fragmentation_in_percent > 20'DECLARE cList CURSORFOR SELECT * FROM #FragOPEN cListFETCH NEXT FROM cListINTO @DBName, @TableName,@SchemaName,@IndexName,@PctFragWHILE @@FETCH_STATUS = 0BEGINIF @PctFrag BETWEEN 20.0 AND 40.0BEGINSET @Defrag = N'ALTER INDEX ' + @IndexName + ' ON ' + @DBName + '.' + @SchemaName + '.' + @TableName + ' REORGANIZE'EXEC sp_executesql @DefragENDELSE IF @PctFrag > 40.0BEGINSET @Defrag = N'ALTER INDEX ' + @IndexName + ' ON ' + @DBName + '.' + @SchemaName + '.' + @TableName + ' REBUILD'EXEC sp_executesql @DefragENDFETCH NEXT FROM cListINTO @DBName, @TableName,@SchemaName,@IndexName,@PctFragENDCLOSE cListDEALLOCATE cListDROP TABLE #Frag |
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2012-12-02 : 10:50:11
|
quote: Originally posted by cubehead Hey all,I'm using the script below to re-org or rebuild indexes in all my SQL 2005 production databases. Everything works great until I tried to run it on a server which has a db that has a dash "-" in its name, then it craps out with a "Could not locate entry in sysdatabases for database 'partial_DB_name_before_the_dash'. No entry found with that name. Make sure that the name is entered correctly." I have been trying different things but none works so far. Any ideas on how to fix this?Thanks very much.DECLARE @DBName NVARCHAR(255),@TableName NVARCHAR(255),@SchemaName NVARCHAR(255),@IndexName NVARCHAR(255),@PctFrag DECIMALDECLARE @Defrag NVARCHAR(MAX)CREATE TABLE #Frag(DBName NVARCHAR(255),TableName NVARCHAR(255),SchemaName NVARCHAR(255),IndexName NVARCHAR(255),AvgFragment DECIMAL)EXEC sp_msforeachdb 'Use ?;INSERT INTO #Frag (DBName,TableName,SchemaName,IndexName,AvgFragment)Select db_name() ,object_name(s.object_id) As ObjectName ,object_name(s.object_id) As SchemaName ,i.Name As IndexName ,s.avg_fragmentation_in_percent From sys.dm_db_index_physical_stats(db_id(), Null, Null, Null, ''Sampled'') s Join sys.indexes i On i.object_id = s.object_id And i.index_id = s.index_id Where i.index_id > 0 And i.index_id < 255 And s.avg_fragmentation_in_percent > 20'DECLARE cList CURSORFOR SELECT * FROM #FragOPEN cListFETCH NEXT FROM cListINTO @DBName, @TableName,@SchemaName,@IndexName,@PctFragWHILE @@FETCH_STATUS = 0BEGINIF @PctFrag BETWEEN 20.0 AND 40.0BEGINSET @Defrag = N'ALTER INDEX ' + @IndexName + ' ON ' + '[' @DBName + ']' + '.' + @SchemaName + '.' + @TableName + ' REORGANIZE'EXEC sp_executesql @DefragENDELSE IF @PctFrag > 40.0BEGINSET @Defrag = N'ALTER INDEX ' + @IndexName + ' ON ' + '[' + @DBName + ']' + '.' + @SchemaName + '.' + @TableName + ' REBUILD'EXEC sp_executesql @DefragENDFETCH NEXT FROM cListINTO @DBName, @TableName,@SchemaName,@IndexName,@PctFragENDCLOSE cListDEALLOCATE cListDROP TABLE #Frag
Try with the red one. Also check Tara's script for it |
|
|
cubehead
Starting Member
4 Posts |
Posted - 2012-12-02 : 11:00:52
|
sodeep,Thank you for the quick response. It didn't work with that fix. I think the problem occurs before it got to that point. I suspect it's happening at the "use ?" part. Could you post a link to Tara's script?Thanks very much. |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2012-12-02 : 11:13:01
|
Forgot to mention. Put bracket around ? like [] |
|
|
cubehead
Starting Member
4 Posts |
Posted - 2012-12-02 : 17:44:34
|
sodeep,Thanks for the help. That took care of it. |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2012-12-02 : 19:38:23
|
Welcome |
|
|
|
|
|
|
|