Select the first 3 rows of each table in a database

前端 未结 2 691
挽巷
挽巷 2020-12-06 06:49

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

I can do it per table with:

SELECT TOP 3 *         


        
相关标签:
2条回答
  • 2020-12-06 07:04

    Here you have:

    DECLARE @sql VARCHAR(MAX)='';
    SELECT @sql=@sql+'SELECT TOP 3 * FROM '+'['+SCHEMA_NAME(schema_id)+'].['+name+']'+';'
      FROM sys.tables
    EXEC(@sql)
    
    0 讨论(0)
  • 2020-12-06 07:20
     exec sp_MSforeachtable 'select top 3 * from ?'
    
    0 讨论(0)
提交回复
热议问题