View top 5 rows in all views

A few years ago I did a blog on viewing sample rows in all used tables.
 
Here is another similar query which allows you to peek at the top 5 rows in each view.

SELECT 'SELECT TOP 5 ''[' + SCHEMA_NAME([schema_id]) + '].[' + [name] + ']'' AS view_name, * FROM [' + DB_NAME() + '].[' + SCHEMA_NAME([schema_id])+ '].[' + [name] + '] WITH (NOLOCK);'  
FROM sys.all_views
ORDER BY [name];

image1
If you are using Azure SQL Database, then use the following which removes the use of the NOLOCK hint:

SELECT 'SELECT TOP 5 ''[' + SCHEMA_NAME([schema_id]) + '].[' + [name] + ']'' AS view_name, * FROM [' + DB_NAME() + '].[' + SCHEMA_NAME([schema_id])+ '].[' + [name] + '];'  
FROM sys.all_views
ORDER BY [name];

Leave a Reply