Select the first 3 rows of each table in the database

I have a database with 69 tables, and I want to select only the first three records of each table.

I can do this at the table with:

SELECT TOP 3 * FROM table_schema.table_name 

However, if I did it manually, it would take a lot of time.

Could you suggest a workaround?

I tried this solution , but I can make it work (I don't know how to change it for MSSQL)

EDIT Thank you for your answers. I probably wasn’t clear enough: I meant that I wanted to parse each individual table and get only the 3 best entries than moving on to the next. Yaroslavl code below is what I need

 DECLARE @sql VARCHAR(MAX)=''; SELECT @ sql=@sql +'SELECT TOP 3 * FROM '+'['+SCHEMA_NAME(schema_id)+'].['+name+']'+';' FROM sys.tables EXEC(@sql) 
+6
source share
2 answers

Here you have:

 DECLARE @sql VARCHAR(MAX)=''; SELECT @ sql=@sql +'SELECT TOP 3 * FROM '+'['+SCHEMA_NAME(schema_id)+'].['+name+']'+';' FROM sys.tables EXEC(@sql) 
+10
source
  exec sp_MSforeachtable 'select top 3 * from ?' 
+5
source

Source: https://habr.com/ru/post/921769/


All Articles