查N个表有多少行 怎么用一条语句搞定?
如何显示成这样子呢
[解决办法]
USE CSDN
GO
DECLARE @sql NVARCHAR(MAX), @fieldlist NVARCHAR(MAX)
SET @fieldlist = ',[a],[aa],[aaaa]'--或者把表名直接写到字符串里,注意表名需要用[]括起来
SET @sql = N'
SELECT * FROM
(
SELECT
A.NAME,
MaxRows = MAX(B.rows)
FROM sys.tables A
INNER JOIN sys.partitions B
ON A.object_id = B.object_id
GROUP BY A.name
) a
PIVOT
(max(maxrows) FOR name IN('+ STUFF(@fieldlist, 1,1, '') +')) b
'
EXEC (@sql)
/*
Aaaaaaa
401
*/