Disclaimer:Hey Guys, this post contains affiliate link to help our reader to buy best product\service for them. It helps us because we receive compensation for our time and expenses.

Infrastructure: This has been run and tested successfully on SQL 2005 and onward.

Scenario:  There might be few cases where you as a DBA need to rebuild heap tables and assume you found out many fragmented heap tables in the databases (queries are available on google search) and you need to rebuild those.

In this case, rebuilding heap tables with the help of cursor can be much handy.

Resolution: 

To know and understand about the heap table click here

Please click here to download rebuild heap tables script.

Hope this helps!