/*获取用户表信息光标*/
SET NOCOUNT ON
DECLARE find_user_table CURSOR
FOR
SELECT [name],crdate FROM sysobjects
WHERE type='U' /*这里只查询用户定义的表*/
ORDER BY [name]
DECLARE @cName VARCHAR(128) /*定义储存表名变量*/
DECLARE @crdate DATETIME /*定义表创建日期变量*/
DECLARE @Rows INT /*定义表行数变量*/
CREATE TABLE #tmpTable /*创建用来储存信息的临时表*/
(
Tablename VARCHAR(128),
crDate datetime,
Row INT
)
OPEN find_user_table /*打开游标*/
FETCH NEXT FROM find_user_table INTO @cName,@crdate /*从光标中读取表名到变量*/
WHILE @@FETCH_STATUS=0
BEGIN
DECLARE @cSql nvarchar(500),@par nvarchar(30)
SELECT @par='@nRows INT OUTPUT'
SELECT @cSql='SELECT @nRows=COUNT(*) FROM ['+@cName+']'
EXECUTE sp_executesql @cSql,@par,@rows OUTPUT /*计算当前表的总行数*/
INSERT INTO #tmpTable valueS(@cName,@crdate,@rows) /*将当前表信息存储到临时表*/
FETCH NEXT FROM find_user_table INTO @cName,@crdate /*从光标中读取表名到变量*/
END
SELECT * FROM #tmpTable /*显示所有表信息*/
DROP TABLE #tmpTable /*删除临时表*/
CLOSE find_user_table /*关闭游标*/
DEALLOCATE find_user_table /*释放游标*/