Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Run this <a href="https://gist.github.com/dalenewman/6377911" rel="noreferrer">updated script</a> to create a stored procedure called <code>MoveIndexToFileGroup</code>. This procedure moves all the non-clustered indexes on a table to a specified file group. It even supports the <code>INCLUDE</code> columns that some other scripts do not. In addition, it will not rebuild or move an index that is already on the desired file group. Once you've created the procedure, call it like this:</p> <pre><code>EXEC MoveIndexToFileGroup @DBName = '&lt;your database name&gt;', @SchemaName = '&lt;schema name that defaults to dbo&gt;', @ObjectNameList = '&lt;a table or list of tables&gt;', @IndexName = '&lt;an index or NULL for all of them&gt;', @FileGroupName = '&lt;the target file group&gt;'; </code></pre> <p>To create a script that will run this for each table in your database, switch your query output to text, and run this:</p> <pre><code>SELECT 'EXEC MoveIndexToFileGroup ''' +TABLE_CATALOG+''',''' +TABLE_SCHEMA+''',''' +TABLE_NAME+''',NULL,''the target file group'';' +char(13)+char(10) +'GO'+char(13)+char(10) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' ORDER BY TABLE_SCHEMA, TABLE_NAME; </code></pre> <p>Please refer to the original <a href="http://blogs.msdn.com/b/ramoji/archive/2008/09/26/how-to-move-existing-indexes-from-one-filegroup-to-another.aspx" rel="noreferrer">blog</a> for more details. I did not write this procedure, but updated it according to the blog's responses and confirmed it works on both SQL Server 2005 and 2008.</p> <h3>Updates</h3> <ol> <li>@psteffek <a href="https://gist.github.com/psteffek/6877012" rel="noreferrer">modified the script</a> to work on SQL Server 2012. I merged his changes.</li> <li>The procedure fails when your table has the <code>IGNORE_DUP_KEY</code> option on. No fix for this yet.</li> <li>@srutzky pointed out the procedure does not guarantee to preserve the order of an index and made suggestions on how to fix it. I updated the procedure accordingly.</li> <li><a href="https://github.com/ojiNY" rel="noreferrer">ojiNY</a> noted the procedure left out index filters (for compatibility with SQL 2005). Per his suggestion, I added them back in.</li> </ol>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload