SELECT CAST(NULL AS sysname) AS 表名称, 1 AS 记录数 INTO #table WHERE 1 = 0 DECLARE @TableName sysname DECLARE testcur CURSOR FOR SELECT [name] FROM sysobjects WHERE xtype ='U' ORDER BY [name] OPEN testcur FETCH NEXT FROM testcur INTO @TableName WHILE @@fetch_status = 0 BEGIN EXEC('INSERT INTO #table SELECT ''' + @TableName + ''', (SELECT COUNT(1) FROM ' + @TableName + ')') FETCH NEXT FROM testcur INTO @TableName END CLOSE testcur DEALLOCATE testcur SELECT * from #table DROP TABLE #table
查询结果如下: