# 一、统计 ## 1.1 数据量统计 ```sql exec sp_spaceused 表名; ``` ## 1.2 批量统计 本质是循环 ```sql exec sp_MSforeachtable "exec sp_spaceused '?'" ``` # 二、分页 ## 2.1 topN 查看前5条 ```sql select TOP 5 *from T_ProductMain ``` ## 2.2 分页查询 要查询上述结果中第 7 条到第 9 条记录 ```sql select top 3 id from tablename where id not in (   select top 6 id from tablename ) ``` m-1=6,m=7 ```sql select top (n-m+1) id from tablename where id not in (   select top m-1 id from tablename ) ``` 通过行号分页 ```sql SELECT * FROM ( SELECT ROW_NUMBER()over(order by getdate()) AS rownumber,* from T_ProductMain ) AS #a WHERE #a.rownumber>=1 AND #a.rownumber<=5 ``` sqlserver 2012及以上版本支持 ```sql SELECT * FROM T_AttrPart ORDER BY PartNo offset 0 ROWS FETCH NEXT 10 ROWS ONLY ``` # 三、元数据 ## 3.1 查看所有表名 ```sql select top 10000 ROW_NUMBER() OVER (ORDER BY a.name) AS No, a.name AS 表名, CONVERT(NVARCHAR(100),isnull(g.[value],'-')) AS 说明 from sys.tables a left join sys.extended_properties g on (a.object_id = g.major_id AND g.minor_id = 0); ``` ## 3.2 查看字段及类型 ```sql select column_name name,data_type type from information_schema.columns where table_name = 'T_AltAffix' ``` ## 3.3 查看数据库版本 ```sql SELECT SERVERPROPERTY('productversion') as version_code, SERVERPROPERTY ('productlevel') as version_level, SERVERPROPERTY ('edition') as version_type ``` ## 3.4 系统自带 + 存储过程 ```sql dt_addtosourcecontrol dt_addtosourcecontrol_u dt_adduserobject dt_adduserobject_vcs dt_checkinobject dt_checkinobject_u dt_checkoutobject dt_checkoutobject_u dt_droppropertiesbyid dt_dropuserobjectbyid dt_generateansiname dt_getobjwithprop dt_getobjwithprop_u dt_getpropertiesbyid dt_getpropertiesbyid_u dt_getpropertiesbyid_vcs dt_getpropertiesbyid_vcs_u dt_isundersourcecontrol dt_isundersourcecontrol_u dt_removefromsourcecontrol dt_setpropertybyid dt_setpropertybyid_u dt_validateloginparams dt_validateloginparams_u dt_vcsenabled dt_verstamp006 dt_verstamp007 dt_whocheckedout dt_whocheckedout_u ``` ## 3.5 查看字符集 ```sql SELECT COLLATIONPROPERTY('Chinese_PRC_Stroke_CI_AI_KS_WS', 'CodePage'); select SERVERPROPERTY('Sqlcharsetname'); ``` | 字符集编码 | 类型 | | ---------- | --------------- | | 936 | 简体中文GBK | | 950 | 繁体中文BIG5 | | 437 | 美国/加拿大英语 | | 932 | 日文 | | 949 | 韩文 | | 866 | 俄文 | | 65001 | unicode UFT-8 | ## 3.6 查看数据占用大小 ```sql SELECT a.name [文件名称] ,cast(a.[size]*1.0/128 as decimal(12,1)) AS [文件设置大小(MB)] , CAST( fileproperty(s.name,'SpaceUsed')/(8*16.0) AS DECIMAL(12,1)) AS [文件所占空间(MB)] , CAST( (fileproperty(s.name,'SpaceUsed')/(8*16.0))/(s.size/(8*16.0))*100.0 AS DECIMAL(12,1)) AS [所占空间率%] , CASE WHEN A.growth =0 THEN '文件大小固定,不会增长' ELSE '文件将自动增长' end [增长模式] ,CASE WHEN A.growth > 0 AND is_percent_growth = 0 THEN '增量为固定大小' WHEN A.growth > 0 AND is_percent_growth = 1 THEN '增量将用整数百分比表示' ELSE '文件大小固定,不会增长' END AS [增量模式] , CASE WHEN A.growth > 0 AND is_percent_growth = 0 THEN cast(cast(a.growth*1.0/128as decimal(12,0)) AS VARCHAR)+'MB' WHEN A.growth > 0 AND is_percent_growth = 1 THEN cast(cast(a.growth AS decimal(12,0)) AS VARCHAR)+'%' ELSE '文件大小固定,不会增长' end AS [增长值(%或MB)] , a.physical_name AS [文件所在目录] ,a.type_desc AS [文件类型] FROM sys.database_files a INNER JOIN sys.sysfiles AS s ON a.[file_id]=s.fileid LEFT JOIN sys.dm_db_file_space_usage b ON a.[file_id]=b.[file_id] ORDER BY a.[type] ``` # 四、视图 + 索引视图 ```sql # 创建索引视图,必须指定列名 create view vwi_T_AttrPart WITH SCHEMABINDING as SELECT ROW_NUMBER()over(order by getdate()) AS rownumber,PartNo ,ParentNO ,PartName ,SectNo ,OrderNo ,SessionCode ,IsNew ,AltMainNo ,PayPartID ,EngineerTypeID1 ,EngineerTypeID2 ,EngineerTypeID3 ,EngineerID ,SectDetailID ,StartPile ,EndPile ,CenterPile ,Mileage ,PartTypeID ,PartRemarks from dbo.T_AttrPart # 添加索引列(报错:不能在索引或统计信息中使用 视图'dbo.vwi_T_AttrPart' 的列 'rownumber',也不能将该列用作分区键,因为它不具有确定性) CREATE UNIQUE CLUSTERED INDEX ucidx_rownumber ON dbo.vwi_T_AttrPart(rownumber); ```