zoukankan      html  css  js  c++  java
  • sqlserver查询表空间大小,使用大小,数据条数

    查询表空间大小,使用大小,总记录数

    DROP TABLE #temp
    CREATE TABLE #temp
        (
          name VARCHAR(100),
          rows INT ,
          reserved varchar(20),
          data varchar(20),
          index_size varchar(20),
          unused varchar(20)
          
        )
      
    DECLARE @tablename VARCHAR(100)
    DECLARE @sql VARCHAR(500)
    DECLARE @str VARCHAR(100)
    DECLARE tablecursor CURSOR
    FOR
        SELECT  name
        FROM    sysobjects
        WHERE   xtype = 'u' 
    OPEN tablecursor
    FETCH NEXT FROM tablecursor INTO @tablename
    WHILE @@fetch_status = 0 
        BEGIN
            SET @str = @tablename
            SET @sql = 'insert into  #temp EXEC sp_spaceused ' + @tablename  
            EXEC(@sql) 
            FETCH NEXT FROM tablecursor INTO @tablename
        END
    CLOSE tablecursor
    DEALLOCATE tablecursor
    SELECT  *
    FROM    #temp 
    

      查询表记录数:

     
    DROP TABLE #temp
    CREATE TABLE #temp
        (
          Recordcount INT ,
          tableName VARCHAR(100)
        )
        
    DECLARE @tablename VARCHAR(100)
    DECLARE @sql VARCHAR(500)
    DECLARE @str VARCHAR(100)
    DECLARE tablecursor CURSOR
    FOR
        SELECT  name
        FROM    sysobjects
        WHERE   xtype = 'u' 
    OPEN tablecursor
    FETCH NEXT FROM tablecursor INTO @tablename
    WHILE @@fetch_status = 0 
        BEGIN
            SET @str = @tablename
            SET @sql = 'insert into #temp(recordcount,tablename) select count(*),'
                + '''' + @tablename + '''' + ' from ' + @tablename
            EXEC(@sql) 
            FETCH NEXT FROM tablecursor INTO @tablename
        END
    CLOSE tablecursor
    DEALLOCATE tablecursor
    SELECT  *
    FROM    #temp  ORDER BY Recordcount
    

      

  • 相关阅读:
    CCF CSP 201509-1 数列分段
    CCF CSP 201503-1 图像旋转 (降维)
    CCF CSP 201412-1 门禁系统
    CCF CSP 201409-1 相邻数对
    CCF CSP 201403-1 相反数
    CCF CSP 201312-1 出现次数最多的数
    Win10环境下 HTTP 错误 500.19
    牛客网 整数拆分 (动态规划)
    牛客网 放苹果
    LeetCode9 回文数
  • 原文地址:https://www.cnblogs.com/Don/p/2835962.html
Copyright © 2011-2022 走看看