Can't find table/object error when running an index rebuild I am running a index rebuild script:

DECLARE @TableName varchar(255)

DECLARE TableCursor CURSOR FOR
SELECT table_name FROM information_schema.tables
WHERE table_type = 'base table'

OPEN TableCursor
FETCH NEXT FROM TableCursor INTO @TableName
WHILE @@FETCH_STATUS = 0

BEGIN

    DBCC DBREINDEX(@TableName,' ',90)

    FETCH NEXT FROM TableCursor INTO @TableName

END

CLOSE TableCursor;
DEALLOCATE TableCursor;


but it fails with the error "Msg 2501, Level 16, State 45, Line 13
Cannot find a table or object with the name "ServiceDeploymentsTable". Check the system catalog."

The script is from https://gallery.technet.microsoft.com/scriptcenter/Script-for-rebuilding-all-8d079754

I have a felling the table might belong to a different object (like dbo or something else) but I don't know how to confirm that or modify the script to deal with that. Or maybe I am compleatly off base.

How can I best deal with this error and rebuild all the indexs in the database?
The chances are the the table you are attempting to reindex exists in a different schema than the default, and as such you will not find it.

The following update should allow you to obtain that table.

DECLARE @TableName nvarchar(512)

DECLARE TableCursor CURSOR FOR
SELECT QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME) FROM information_schema.tables
WHERE table_type = 'base table'

OPEN TableCursor
FETCH NEXT FROM TableCursor INTO @TableName
WHILE @@FETCH_STATUS = 0

BEGIN

    DBCC DBREINDEX(@TableName,' ',90)

    FETCH NEXT FROM TableCursor INTO @TableName

END

CLOSE TableCursor;
DEALLOCATE TableCursor;


A couple of things that you should note, DBCC DBREINDEX is deprecated, you should really be using ALTER INDEX REBUILD instead. As an additional item, rather than trying to reinvent the wheel there are free solutions out there that have been written by folks with many years of experience, such as Minionware Reindex. These are designed to be quickly and easily deployed and to provide simple management.