Sqlserver
 sql >> Base de Dados >  >> RDS >> Sqlserver

Script de inventário de coleção do SQL Server -1


Oi,

Você deseja aprender todo o Inventário do SQL Server ao se conectar ao banco de dados do SQL Server pela primeira vez.







Este SQL Server Database Inventory consiste nas seguintes informações.
  • Nome da máquina
  • Nome da instância
  • Tamanho total do log do banco de dados
  • Total de registro do banco de dados usado
  • Tamanho total do arquivo de dados do banco de dados
  • Contagem lógica de CPU
  • Contagem física de CPU
  • Memória física no servidor
  • Tamanho total do disco no servidor
  • Total de espaço livre em disco no servidor

O resultado da consulta será como as capturas de tela a seguir.









A consulta é como seguir, simplifica muito seu trabalho quando você se conecta a qualquer banco de dados SQL Server pela primeira vez.


sp_configure 'show advanced options', 1;
GO
RECONFIGURE;
GO
sp_configure 'Ole Automation Procedures', 1;
GO
RECONFIGURE;
GO
/*******************************************************/
SET NOCOUNT ON
DECLARE @hr int
DECLARE @fso int
DECLARE @drive char(1)
DECLARE @odrive int
DECLARE @TotalSize varchar(20) DECLARE @MB Numeric ; SET @MB = 1048576

CREATE TABLE #drives (drive char(1) PRIMARY KEY, FreeSpace int NULL,
TotalSize int NULL) INSERT #drives(drive,FreeSpace) EXEC
master.dbo.xp_fixeddrives EXEC @hr=sp_OACreate
'Scripting.FileSystemObject',@fso OUT IF @hr <> 0 EXEC sp_OAGetErrorInfo
@fso

DECLARE dcur CURSOR LOCAL FAST_FORWARD
FOR SELECT drive from #drives ORDER by drive
OPEN dcur FETCH NEXT FROM dcur INTO @drive

WHILE @@FETCH_STATUS=0
BEGIN
EXEC @hr = sp_OAMethod @fso,'GetDrive', @odrive OUT, @drive
IF @hr <> 0 EXEC sp_OAGetErrorInfo @fso EXEC @hr =
sp_OAGetProperty
@odrive,'TotalSize', @TotalSize OUT IF @hr <> 0 EXEC sp_OAGetErrorInfo
@odrive UPDATE #drives SET [email protected]/@MB WHERE
[email protected] FETCH NEXT FROM dcur INTO @drive
End
Close dcur
DEALLOCATE dcur

EXEC @hr=sp_OADestroy @fso IF @hr <> 0 EXEC sp_OAGetErrorInfo @fso
--SELECT @@Servername
--SELECT
--drive, TotalSize as 'Total(MB)', FreeSpace as 'Free(MB)' FROM #drives
--ORDER BY drive

CREATE TABLE #CPUInfo
( Logical_CPU_Count bigint,
Hyperthread_Ratio bigint,
Physical_CPU_Count bigint,
Physical_Memory_MB bigint
)

INSERT INTO #CPUInfo(
Logical_CPU_Count,
Hyperthread_Ratio,
Physical_CPU_Count,
Physical_Memory_MB
)
SELECT 
cpu_count AS [Logical_CPU_Count] 
,hyperthread_ratio AS [Hyperthread_Ratio]
,cpu_count/hyperthread_ratio AS [Physical_CPU_Count]
, physical_memory_kb/1024 AS [Physical_Memory_MB]
FROM sys.dm_os_sys_info

CREATE TABLE #DatabaseInfo
( Machine_Name varchar(50),
Instance_Name varchar(50),
Sql_Server_Name varchar(50),
Total_Database_log_size_MB bigint,
Total_Database_log_used_MB bigint,
Total_Database_Data_File_Size_MB bigint
)
INSERT INTO #DatabaseInfo
(
Machine_Name,
Instance_Name,
Sql_Server_Name,
Total_Database_log_size_MB,
Total_Database_log_used_MB,
Total_Database_Data_File_Size_MB
)


select convert(varchar(50),serverproperty('MachineName')) 'Machine_Name'
,convert(varchar(50),isnull(serverproperty('InstanceName'),'mssqlserver')) 'Instance_Name'
,convert(varchar(50),@@SERVERNAME) 'Sql_Server_Name'
,sum(ls.cntr_value/1024) as [Total_Database_log_size_MB]
,sum(lu.cntr_value/1024)as [Total_Database_log_used_MB]
,sum(ds.cntr_value/1024) as [Total_Database_Data_File_Size_MB]
from sys.databases d
left outer join sys.dm_os_performance_counters as lu on lu.instance_name=d.name and lu.counter_name like N'Log File(s) Used Size (KB)%'
left outer join sys.dm_os_performance_counters as ls on ls.instance_name=d.name and ls.counter_name like N'Log File(s) Size (KB)%' and ls.cntr_value > 0
left outer join sys.dm_os_performance_counters as lp on lp.instance_name=d.name and lp.counter_name like N'Percent Log Used%'
left outer join sys.dm_os_performance_counters as ds on ds.instance_name=d.name and ds.counter_name like N'Data File(s) Size (KB)%'
where d.database_id>4; -- sistem database ler harc


WITH SizeDisc AS
( -- sunucu üzerindeki tüm drive size ve free size bilgisi
SELECT SUM(TotalSize) as 'TotalDiscSizeonServer(MB)', 
SUM(FreeSpace) as 'TotalFreeDiscSizeOnServer(MB)' 
FROM #drives
)
SELECT *
FROM #DatabaseInfo,#CPUInfo,SizeDisc

DROP TABLE #Drives 
DROP TABLE #DatabaseInfo
DROP TABLE #CPUInfo 
GO
/*******************************************************/
/* Disabling Ole Automation Procedures */
sp_configure 'show advanced options', 1;
GO
RECONFIGURE;
GO
sp_configure 'Ole Automation Procedures', 0;
GO
RECONFIGURE;
/*******************************************************/
GO