• 列出Server上5张最大的表


            遍历所有数据库,但不包括系统数据库。列出表所在数库名,表名,表的大小,表的记录数。非常有趣,T-SQL 看下面:

       1:  create table #t(d nvarchar(MAX),t nvarchar(MAX),r int,x nvarchar(100),s nvarchar(100),y nvarchar(100),z nvarchar(100))
       2:  declare @s nvarchar(MAX)
       3:  set @s=replace('if !~! not in (!master!,!model!,!msdb!,!tempdb!) exec [~].dbo.sp_msforeachtable "insert into #t(t, r,x,s,y,z) exec [~].dbo.sp_spaceused !?!"','!',char(39))
       4:  EXEC sp_MSForEachDB @command1=@s, @command2="update #t set d='~' where d is null", @replacechar='~'
       5:  select top(5) d as base, t as [table], s as size, r as rows from #t order by Cast(LEFT(s,len(s)-3) as int) desc
       6:  drop table #t

           结果类似如下:

    BASE     TABLE   SIZE        ROWS      
    ----------------------------------
    base1    T1      162720 KB   14233       
    base2    T2      38000 KB    8462       
    base1    T3      10720 KB    5343        
    base1    T4      9040 KB     5006       
    base3    T5      2800 KB     1899  
     
     
    Reference:TSQL Challenge 7
     
    希望对您有所帮助
    Author: Petter Liu  http://wintersun.cnblogs.com 
  • 相关阅读:
    通讯技术
    (1)sqlserver2017安装
    c# api身份验证和授权
    ()centos7 安装python36
    python 包管理
    ()centos7 安装mysql8.0
    [bzoj1095][ZJOI2007]Hide 捉迷藏 点分树,动态点分治
    bzoj 3544 [ONTAK2010]Creative Accounting 贪心
    BZOJ4300 绝世好题 dp
    bzoj 4295 [PA2015]Hazard 贪心,暴力
  • 原文地址:https://www.cnblogs.com/wintersun/p/1537006.html
Copyright © 2020-2023  润新知