Sql-server – Moving all Heap Tables to another Filegroup in SQL Server

filegroupsnonclustered-indexscriptingsql serversql-server-2017

In SQL Server 2017, I want to move ALL Heaps in a DB to a secondary filegroup. What's the easiest way to do this?

This question is only for Nonclustered Indexes.

https://stackoverflow.com/questions/4237238/moving-all-non-clustered-indexes-to-another-filegroup-in-sql-server

In the process of optimizing an older legacy database.

  • I want to write a script to move all heap tables with a cursor loop similar to the Stack Overflow link above.

Best Answer

As far as I know, you have two options:

  • Physically move the data. Create a new empty heap in the secondary filegroup / Copy the data / rename the object, everything inside of implicit transaction. And don't forget to script out indexes, constraints etc.
  • Create a clustered index in the secondary filegroup, and then drop it.

I'd prefer the first option.