title: Loop a query which use multiple Databases using a cursor
body: <p>I am trying to loop a query through multiple databases that I have listed in a table, I've tried to use a cursor and switch out the USE statement with the new database name but I keep getting an Incorrect syntax near '@DBName'. Is there any way to do this? Below is my attempt.</p>
  
  <pre><code>DECLARE  @DBName  varchar (100)
  DECLARE  @Sql  varchar (100)
  DECLARE  @Table   varchar (100) 
  DECLARE  @IndexName varchar (100)
  DECLARE TableCursor CURSOR FOR
  select DBName,[Table],IndexName from IndexOverview_FragLevels
  OPEN TableCursor            
  FETCH NEXT FROM TableCursor INTO @DBName, @Table, @IndexName                                                                                            
  WHILE @@FETCH_STATUS = 0
  BEGIN
  use @DBName;
  DBCC DBREINDEX (@Table ,@IndexName, 90);
  FETCH NEXT FROM TableCursor INTO @DBName,@Table,@IndexName
  END
  close TableCursor
  DEALLOCATE TableCursor
  </code></pre>
  
  <p>In this code I am reindexing the tables from multiple databases.In which  data is stores in a tables called IndexOverview_FragLevels</p>