USE pubs
GO
DECLARE @NAME VARCHAR(40)
DECLARE @SQL VARCHAR(4000)
DECLARE authors_cursor CURSOR FOR
SELECT Name as 表名 FROM SYSOBJECTS
WHERE XTYPE='U'
OPEN authors_cursor
FETCH NEXT FROM authors_cursor
INTO @NAME
SET @SQL='SELECT '''+@NAME+''' AS 表名, COUNT(1) AS 行数 FROM '+@NAME
WHILE @@FETCH_STATUS = 0
BEGIN
FETCH NEXT FROM authors_cursor
INTO @NAME
SET @SQL=@SQL+' UNION ALL SELECT '''+@NAME+
''' AS 表名,COUNT(1)AS 行数 FROM '+@NAME
END
CLOSE authors_cursor
DEALLOCATE authors_cursor
EXEC(@SQL)
GO