-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathSQLInventoryObjects.sql
203 lines (181 loc) · 5.62 KB
/
SQLInventoryObjects.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/**************************************
Author: Michael S. Fal (http://www.mikefal.net)
Finalized: 2012-04-16
Creates server inventory SQL objects. Should be created in
an administrative database ([MSFAdmin]).
***************************************/
IF NOT EXISTS (SELECT 1 from sys.databases WHERE name = 'MSFADMIN')
CREATE DATABASE [MSFADMIN];
go
USE [MSFADMIN];
go
if exists (select * from sys.tables where name = 'MachineInventory')
drop table [dbo].[MachineInventory];
CREATE TABLE [dbo].[MachineInventory](
[ServerName] [varchar](200) NOT NULL,
[Model] [varchar](100) NULL,
[Manufacturer] [varchar](100) NULL,
[Architechture] [varchar](10) NULL,
[PhysicalCPUS] [int] NULL,
[LogicalCPUS] [int] NULL,
[MaxSpeed] [int] NULL,
[Memory] [int] NULL,
[OSName] [varchar](100) NULL,
[OSVersion] [varchar](100) NULL,
[OSEdition] [varchar](100) NULL,
[SPVersion] [varchar](10) NULL,
[Cluster] [varchar](100) NULL,
[LastModified] [smalldatetime] DEFAULT GETDATE(),
[LastModifiedUser] [sysname],
CONSTRAINT [PK_Server] PRIMARY KEY CLUSTERED ([ServerName])
);
if exists (select * from sys.tables where name = 'InstanceInventory')
drop table [dbo].[InstanceInventory];
CREATE TABLE [dbo].[InstanceInventory](
[ServerName] [varchar](200) NOT NULL,
[InstanceName] [varchar](200) NOT NULL,
[SQLName] AS (case when [InstanceName] IS NULL then [ServerName] else ([ServerName]+'\')+[InstanceName] end),
[SQLVersion] [varchar](20) NULL,
[SQLVersionDesc] [varchar](20) NULL,
[SQLEdition] [varchar](50) NULL,
[IP] [varchar](20) NULL,
[Port] [int] NULL,
[MemoryMinMB] [int] NULL,
[MemoryMaxMB] [int] NULL,
[MAXDOPVal] [int] NULL,
[LastModified] [smalldatetime] NULL DEFAULT GETDATE(),
[LastModifiedUser] [sysname] NOT NULL DEFAULT SYSTEM_USER,
CONSTRAINT [PK_Instance] PRIMARY KEY CLUSTERED ([ServerName],[InstanceName])
);
go
create schema dataload;
go
if exists (select * from sys.tables where name = 'MachineLoad')
drop table [dataload].[MachineLoad];
CREATE TABLE [dataload].[MachineLoad](
[ServerName] [varchar](200) NOT NULL,
[Model] [varchar](100) NULL,
[Manufacturer] [varchar](100) NULL,
[Architechture] [varchar](10) NULL,
[PhysicalCPUS] [int] NULL,
[LogicalCPUS] [int] NULL,
[MaxSpeed] [int] NULL,
[Memory] [int] NULL,
[OSName] [varchar](100) NULL,
[OSVersion] [varchar](100) NULL,
[SPVersion] [varchar](10) NULL,
[Cluster] [varchar](20) NULL
);
if exists (select * from sys.tables where name = 'InstanceLoad')
drop table [dataload].[InstanceLoad];
CREATE TABLE [dataload].[InstanceLoad](
[ServerName] [varchar](200) NOT NULL,
[InstanceName] [varchar](200) NOT NULL,
[SQLVersion] [varchar](20) NULL,
[SQLVersionDesc] [varchar](20) NULL,
[SQLEdition] [varchar](50) NULL,
[IP] [varchar](20) NULL,
[Port] [varchar](100) NULL,
[MemoryMinMB] [varchar](100) NULL,
[MemoryMaxMB] [varchar](100) NULL,
[MAXDOPVal] [varchar](100) NULL,
[PhysicalHost] [varchar](100) NULL
);
GO
if exists (select * from sys.procedures where name = 'dbasp_ProcessInventory')
drop procedure dbasp_ProcessInventory;
go
create procedure dbasp_ProcessInventory
as
begin
/**************************************
Author: Michael S. Fal (http://www.mikefal.net)
Finalized: 2012-04-16
Pulls data from inventory staging tables and loads it
into live reporting tables. Instance inventory is
updated, machine inventory is deleted and reloaded.
***************************************/
MERGE INTO [dbo].[InstanceInventory] as [Target]
USING [dataload].[InstanceLoad] as [Source]
ON [Target].[ServerName] = [Source].[ServerName]
and [Target].[InstanceName] = [Source].[InstanceName]
WHEN MATCHED THEN
UPDATE
set
SQLVersion = [Source].SQlVersion
,SQLVersionDesc = CASE WHEN [Source].SQLVersion like '12%' then 'SQL 2014 '
WHEN [Source].SQLVersion like '11%' then 'SQL 2012 '
WHEN [Source].SQLVersion like '10.50%' then 'SQL 2008 R2 '
WHEN [Source].SQLVersion like '10.0%' then 'SQL 2008 '
WHEN [Source].SQLVersion like '9%' then 'SQL 2005 '
WHEN [Source].SQLVersion like '8%' then 'SQL 2000 ' END + [Source].SQLVersionDesc
,SQLEdition = [Source].SQLEdition
,IP = [Source].IP
,Port = [Source].Port
,MemoryMinMB = [Source].MemoryMinMB
,MemoryMaxMB = [Source].MemoryMaxMB
,MAXDOPVal = [Source].MAXDOPVal
,LastModified = GETDATE()
,LastModifiedUser = SYSTEM_USER
WHEN NOT MATCHED BY TARGET THEN
INSERT ([ServerName]
,[InstanceName]
,[SQLVersion]
,[SQLVersionDesc]
,[SQLEdition]
,[IP]
,[Port]
,[MemoryMinMB]
,[MemoryMaxMB]
,[MAXDOPVal])
VALUES([Source].[ServerName]
,[Source].[InstanceName]
,[Source].SQlVersion
,CASE WHEN [Source].SQLVersion like '12%' then 'SQL 2014 '
WHEN [Source].SQLVersion like '11%' then 'SQL 2012 '
WHEN [Source].SQLVersion like '10.50%' then 'SQL 2008 R2 '
WHEN [Source].SQLVersion like '10.0%' then 'SQL 2008 '
WHEN [Source].SQLVersion like '9%' then 'SQL 2005 '
WHEN [Source].SQLVersion like '8%' then 'SQL 2000 ' END + [Source].SQLVersionDesc
,[Source].SQLEdition
,[Source].IP
,[Source].Port
,[Source].MemoryMinMB
,[Source].MemoryMaxMB
,[Source].MAXDOPVal);
delete from MachineInventory
where ServerName in (select ServerName from dataload.MachineLoad)
insert into MachineInventory(ServerName,
Model,
Manufacturer,
Architechture,
PhysicalCPUS,
LogicalCPUS,
MaxSpeed,
Memory,
OSName,
OSVersion,
OSEdition,
SPVersion,
Cluster,
LastModified,
LastModifiedUser)
select
Upper(ServerName),
Model,
Manufacturer,
Architechture,
PhysicalCPUS,
case when LogicalCPUS = 0 then PhysicalCPUs else LogicalCPUS end,
MaxSpeed,
Memory,
OSName,
OSVersion,
OSName,
SPVersion,
Cluster,
getdate(),
SYSTEM_USER
from
dataload.MachineLoad
end; --dbasp_ProcessInventory