Select the first 3 rows of each table in a database

前端 未结 2 698
挽巷
挽巷 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)
    

提交回复
热议问题