Monday, October 10, 2011

Could not find a table or object named ''. Check sysobjects. [SQLSTATE 42S02] (Error 2501).

While performing DBREINDEX operation on all tables of a database using the below code

DECLARE @TableName varchar(100)
DECLARE tablename CURSOR FOR
SELECT table_name
FROM information_schema.tables
WHERE table_type = 'base table'
OPEN tablename

FETCH NEXT FROM tablename INTO @TableName
WHILE @@FETCH_STATUS = 0
BEGIN
DBCC DBREINDEX (@TableName, ' ', 80)
FETCH NEXT FROM tablename INTO @TableName
END
CLOSE tablename
DEALLOCATE tablename

The job keeps failing stating a table cannot be found.

Resolution:

Here issue is dbreindex is failing because the table is owned by different schema other than ‘dbo’. This can be avoided by concatenating the schema name while reindexing. So the query can be changed as below while fetching the table name.

select TABLE_SCHEMA+'.'+TABLE_NAME from information_schema.tables

Hope this helps atleast few!!!

No comments:

Post a Comment