Created
April 5, 2022 18:54
-
-
Save Tomamais/798e4c61c219903b4900c1f6d8cefaa5 to your computer and use it in GitHub Desktop.
List all tables with data in a SQL Server Database
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DECLARE @SQL AS VarChar(MAX) | |
SET @SQL = '' | |
SELECT @SQL = @SQL + 'IF EXISTS(SELECT 1 FROM ' + TABLE_SCHEMA + '.[' + TABLE_NAME + ']) SELECT ''' + TABLE_NAME + ''' as TableName;IF EXISTS(SELECT 1 FROM ' + TABLE_SCHEMA + '.[' + TABLE_NAME + ']) SELECT * FROM ' + TABLE_SCHEMA + '.[' + TABLE_NAME + '] --ELSE SELECT ''No records'' AS [' + TABLE_NAME + ']' + CHAR(13) | |
FROM INFORMATION_SCHEMA.TABLES ORDER BY TABLE_NAME | |
EXEC (@SQL) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment