分析ms数据库的用户表数和记录数
2005-07-07 22:13
337 查看
近来一个项目要求做几个数据库的比较,其中有几项指标和数据库的表有关:
用户表个数
最大表记录数
记录总条数
如果靠手工一个表一个表的去查看统计,不仅枯燥费时,而且灵活性和扩展都不是很好,可能主要还是想偷懒的原因吧,今天花了点时间写了个简单的存储过程,只是为了快点完成任务,还没得及考虑方便性和处理错误,下面是源代码,原理很简单,参看代码中注释。
create procedure sp_tableCount
@newTable varchar(50),--new create table name
@isSet int --whether return new table recordset,non 0--return
as
declare @TableName nvarchar(100);
declare @sql nvarchar(800)
SET NOCOUNT ON
----create a target table named @newTable param value--------
IF EXISTS (SELECT table_name FROM INFORMATION_SCHEMA.TABLES
WHERE table_name = @newTable)
exec('DROP TABLE '+@newTable)
-----create target table------------
set @sql='create table ' + @newTable + '
(
Categary nvarchar(100) not null,
Value int
)'
exec(@sql)
----------add user tables count into target table----------------
set @sql='insert into '+@newTable+' select ''User Tables'',count(*)-1 from sysobjects where type=''U'''
exec(@sql)
--------define a cursor pointing the user tablename recordset--------
declare TableName_Cursor CURSOR FOR
select name from sysobjects where type='U'and name<>@newTable
open TableName_Cursor
fetch next from TableName_Cursor into @TableName
-------append every user table recordcount to target table----------------
while @@Fetch_Status=0
begin
set @sql='insert into '+@newTable+' select N'''+@TableName+''',count(*) from ' + @TableName
exec(@sql)
fetch next from TableName_Cursor into @TableName
end
-------release resource occupied by TableName_Cursor --------
close TableName_Cursor
deallocate TableName_Cursor
--------deal with the @isSet param-----------------
if @isSet<>0
exec('select * from '+@newTable)
用户表个数
最大表记录数
记录总条数
如果靠手工一个表一个表的去查看统计,不仅枯燥费时,而且灵活性和扩展都不是很好,可能主要还是想偷懒的原因吧,今天花了点时间写了个简单的存储过程,只是为了快点完成任务,还没得及考虑方便性和处理错误,下面是源代码,原理很简单,参看代码中注释。
create procedure sp_tableCount
@newTable varchar(50),--new create table name
@isSet int --whether return new table recordset,non 0--return
as
declare @TableName nvarchar(100);
declare @sql nvarchar(800)
SET NOCOUNT ON
----create a target table named @newTable param value--------
IF EXISTS (SELECT table_name FROM INFORMATION_SCHEMA.TABLES
WHERE table_name = @newTable)
exec('DROP TABLE '+@newTable)
-----create target table------------
set @sql='create table ' + @newTable + '
(
Categary nvarchar(100) not null,
Value int
)'
exec(@sql)
----------add user tables count into target table----------------
set @sql='insert into '+@newTable+' select ''User Tables'',count(*)-1 from sysobjects where type=''U'''
exec(@sql)
--------define a cursor pointing the user tablename recordset--------
declare TableName_Cursor CURSOR FOR
select name from sysobjects where type='U'and name<>@newTable
open TableName_Cursor
fetch next from TableName_Cursor into @TableName
-------append every user table recordcount to target table----------------
while @@Fetch_Status=0
begin
set @sql='insert into '+@newTable+' select N'''+@TableName+''',count(*) from ' + @TableName
exec(@sql)
fetch next from TableName_Cursor into @TableName
end
-------release resource occupied by TableName_Cursor --------
close TableName_Cursor
deallocate TableName_Cursor
--------deal with the @isSet param-----------------
if @isSet<>0
exec('select * from '+@newTable)
相关文章推荐
- 分析MSSQL数据库的用户表数和记录数 (转载)
- 查询 数据库所有用户表 的记录,对分析数据库表的结构很有用,很方便,贴出来,备用!
- MS-SQL2005服务器登录名、角色、数据库用户、角色、架构的关系
- MS SQL Server数据库事务锁机制分析
- Oracle同一个用户下启动多个数据库实例报错记录
- 快速得到数据库中所有用户表的记录数(行数)
- 如何查询数据库中ID的最新的一条记录 - MS-SQL Server / 基础类
- 基于客户端用户行为记录的网站可用性分析工具研究
- MS-SQL2005服务器登录名、角色、数据库用户、角色、架构的关系
- 多用户共享表时,删除数据库记录时最容易忽略的安全逻辑条件
- [转载]MS SQL Server数据库事务锁机制分析
- 用户行为分析之数据采集【怎么在不影响网站访问效率的基础上,记录用户的历史行为】
- MS SQL Server数据库事务锁机制分析
- 将MS SQL SERVER数据库运行在普通用户(独立用户)状态下的设置方法终结篇
- 快速得到数据库中所有用户表的记录数(行数)
- Oracle查询数据库中当前登录用户所有表的记录数
- 用户行为分析数据库设计
- SQL--统计出所有数据库内用户表的记录总数
- Oracle查询数据库中某用户下满足某些条件的表的记录数
- 数据库-删除表中重复记录实现方法分析