Skip to content

Instantly share code, notes, and snippets.

@howellcc
Created April 8, 2016 17:36
Show Gist options
  • Save howellcc/3abfda745c6de9f69b89472108676073 to your computer and use it in GitHub Desktop.
Save howellcc/3abfda745c6de9f69b89472108676073 to your computer and use it in GitHub Desktop.
[SQL] Reorganize every table in every schema in a database.
USE --DATABASE NAME
DECLARE @TableName varchar(255)
DECLARE @TableSchema varchar(255)
DECLARE @FULLNAME varchar(512)
DECLARE TableCursor CURSOR FOR
SELECT table_name, table_schema FROM information_schema.tables
WHERE table_type = 'base table'
OPEN TableCursor
FETCH NEXT FROM TableCursor INTO @TableName, @TableSchema
WHILE @@FETCH_STATUS = 0
BEGIN
SET @FULLNAME = @TableSchema + '.' + @TableName
DBCC DBREINDEX(@FULLNAME,' ',90)
FETCH NEXT FROM TableCursor INTO @TableName, @TableSchema
END
CLOSE TableCursor
DEALLOCATE TableCursor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment