日期:2014-05-17 浏览次数:20504 次
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)
/*
A aa aaaa
4 0 1
*/