How do I alter index rebuild an entire database?
To do all tables:

EXEC sp_MSForEachTable 'ALTER INDEX ALL ON ? REBUILD'


There is no statement to do all tables in the database like you tried above

However, sometimes you don't want this, say for read only tables or huge tables. Also, some tables/indexes will hardly be fragmented.

A more intelligent way is to check fragmentation first then rebuild or reorganise. One common and widely used example is SQL Fool's script