Skip to content

Instantly share code, notes, and snippets.

@DylanKnevitt
Last active December 18, 2015 02:49
Show Gist options
  • Save DylanKnevitt/5714090 to your computer and use it in GitHub Desktop.
Save DylanKnevitt/5714090 to your computer and use it in GitHub Desktop.
A basic loop to select all columns from all tables/views in a database, useful with the Database Engine Tuning Advisor on SSMS for a very basic idea of what indices and statistics are missing.
DECLARE TOCURSOR CURSOR FOR
SELECT('SELECT * FROM [' + name + '];')
FROM sys.tables
--FROM sys.views
ORDER BY name ASC
DECLARE @query NVARCHAR(MAX)
OPEN TOCURSOR
FETCH NEXT FROM TOCURSOR INTO
@query
WHILE @@FETCH_STATUS = 0
BEGIN
EXEC (@query)
FETCH NEXT FROM TOCURSOR INTO
@query
END
CLOSE TOCURSOR
DEALLOCATE TOCURSOR
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment