SQLServer数据库,表内存,实例名分析SQL语句


--数据库内存分析
USE master
go
DECLARE @insSize TABLE(dbName sysname,checkTime VARCHAR(19),dbSize VARCHAR(50),logSize VARCHAR(50))
INSERT INTO @insSize ( dbName, checkTime, dbSize, logSize )
EXEC sp_msforeachdb 'select ''?'' dbName,CONVERT(VARCHAR(19),GETDATE(),120) checkTime,LTRIM(STR(SUM(CASE WHEN RIGHT(FILENAME,3)<>''ldf'' THEN convert (dec (15,2),size) * 8 / 1024 ELSE 0 END),15,2)+'' MB'') dbSize,  
                 LTRIM(STR(SUM(CASE WHEN RIGHT(FILENAME,3)=''ldf''  THEN convert (dec (15,2),size) * 8 / 1024 ELSE 0 END),15,2)+'' MB'') logSize from ?.dbo.sysfiles'
--SELECT * FROM @insSize ORDER BY CONVERT(DECIMAL,LTRIM(RTRIM(SUBSTRING(dbSize,1,LEN(dbSize)-2)))) DESC
SELECT checkTime AS '检查日期', dbName AS '数据库',dbSize AS '数据容量',logSize AS '日志容量',CAST(CAST(LEFT(dbSize,LEN(dbSize)-2) as decimal(10,2))+CAST(LEFT(logSize,LEN(logSize)-2) as decimal(10,2)) AS VARCHAR(20))+'MB' AS '占用空间' FROM @insSize 
WHERE dbName NOT IN('master','model','tempdb','msdb','distribution')
ORDER BY CONVERT(DECIMAL,LTRIM(RTRIM(SUBSTRING(dbSize,1,LEN(dbSize)-2)))) DESC

--表空间分析
BEGIN
USE[EMPI]
--select count(1) from sysobjects where xtype='U'数据库中表的数量
create table #t(name varchar(255), rows bigint, reserved varchar(20), data varchar(20), index_size varchar(20), unused varchar(20))
exec sp_MSforeachtable "insert into #t exec sp_spaceused '?'" 
select name AS '表名',rows AS '数据行数',CAST(CAST(CAST(LEFT(data,LEN(data)-2) AS FLOAT)/1024 AS DECIMAL(10,3)) AS VARCHAR(20))+'MB' AS '占用空间',
CAST(CAST(CAST(LEFT(reserved,LEN(reserved)-2) AS FLOAT)/1024 AS DECIMAL(10,3)) AS VARCHAR(20))+'MB' AS '保留的总空间',index_size AS '索引的空间',unused AS '未用空间' FROM #t 
ORDER BY CONVERT(DECIMAL,LTRIM(RTRIM(SUBSTRING(data,1,LEN(data)-2)))) DESC
drop table #t
end



--用户名以及权限(不全)
use master
go
SELECT  u.name AS '用户名',g.name AS '权限', u.default_database_name AS '默认数据库'
  from sys.server_principals u, sys.server_principals g, sys.server_role_members m
 where g.principal_id = m.role_principal_id
   and u.principal_id = m.member_principal_id
  order by 1, 2
go
 
--数据库实例名
select @@SERVICENAME

  


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告