zoukankan      html  css  js  c++  java
  • 分析MSSQL数据库的用户表数和记录数 (转载)

    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)

    关于作者: 王昕(QQ:475660) 在广州工作生活30余年。十多年开发经验,在Java、即时通讯、NoSQL、BPM、大数据等领域较有经验。
    目前维护的开源产品:https://gitee.com/475660
  • 相关阅读:
    国内公有云对比(1)- 功能篇
    国内公有云对比(1.1)- 功能篇之新浪云
    人不成熟的六大特征
    HDU1506 Largest Rectangle in a Histogram (动规)
    windows pipe
    LeetCode || Candy
    Spin.js-CSS动画进度载入器
    poj 2932 Coneology (扫描线)
    虚拟存储器--虚拟地址与物理地址
    eclipse下的ssh框架整合过程及測试
  • 原文地址:https://www.cnblogs.com/starcrm/p/1309844.html
Copyright © 2011-2022 走看看