日期:2014-05-17 浏览次数:20742 次
DECLARE CountTableRecords CURSOR READ_ONLY FOR
--我需要在外层加一个数遍历数据库名称的循环.
--根据数据库名称遍历表名的循环.
--在根据表名进行查询数据信息
--这句是获得当前库中所用的用户表面
SELECT sst.name,
Schema_name(sst.schema_id)
FROM sys.tables sst
WHERE sst.TYPE = 'U'
DECLARE @name VARCHAR(80),
@schema VARCHAR(40)
OPEN CountTableRecords
-- 根据表名进行遍历统计表数据信息.
FETCH NEXT FROM CountTableRecords INTO @name, @schema
WHILE ( @@FETCH_STATUS <> -1 )
BEGIN
IF ( @@FETCH_STATUS <> -2 )
BEGIN
PRINT @name
DECLARE @sql NVARCHAR(1024)
SET @sql='DECLARE @count INT SELECT @count=COUNT(1) FROM ' + Quotename(@schema)
+
'.' + Quotename(@name) +' PRINT @count'
EXEC Sp_executesql @sql
END
FETCH NEXT FROM CountTableRecords INTO @name, @schema
END
CLOSE CountTableRecords
DEALLOCATE CountTableRecords
GO
---------------创建表记录表记录
create table tableRecord
(
tableName varchar(50) not null,
countRecord int null,
identRecord int null
)
go
--这是动态读取数据库名称,根据数据库名称获取当前用户表名
create procedure getTableRecord
as
--清空数据
truncate table tableRecord
--定义临时表
declare @tables table(name varchar(50))
declare @databaseName varchar(200)
declare @tbName varchar(200)
declare cursor_database cursor for
select name from [sysdatabases] where crdate>=convert(varchar,ge