一个查看SQL ServerServer数据库空间使用情况的存储过程 SpaceUsed

2022-11-12 09:52:39
内容摘要
这篇文章主要为大家详细介绍了一个查看SQL ServerServer数据库空间使用情况的存储过程 SpaceUsed,具有一定的参考价值,可以用来参考一下。 对此感兴趣的朋友,看看idc笔记做的
文章正文

这篇文章主要为大家详细介绍了一个查看SQL ServerServer数据库空间使用情况的存储过程 SpaceUsed,具有一定的参考价值,可以用来参考一下。

对此感兴趣的朋友,看看idc笔记做的技术笔记!

运行下面存储过程然后直接使用SpaceUsed就可以查看了.存储过程代码程序代码

代码如下:

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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
<code>CreateprocedureSpaceUsed
 
as
 
begin
 
declare@idint--Theobjectidof@objname.
 
declare@typecharacter(2)--Theobjecttype.
 
declare@pagesint--Workingvariableforsizecalc.
 
declare@dbnamesysname
 
declare@dbsizedec(15,0)
 
declare@logsizedec(15)
 
declare@bytesperpagedec(15,0)
 
declare@pagesperMBdec(15,0)
 
declare@objnamenvarchar(776)--Theobjectwewantsizeon.
 
declare@updateusagevarchar(5)--Param.forspecifyingthat
 
createtable#temp1
 
(
 
表名varchar(200)null,
 
行数char(11)null,
 
保留空间varchar(15)null,
 
数据使用空间varchar(15)null,
 
索引使用空间varchar(15)null,
 
未用空间varchar(15)null
 
)
 
--select@objname='N_dep'--usageinfo.shouldbeupdated.
 
select@updateusage='false'
 
/*CreatetemptablesbeforeanyDMLtoensuredynamic
 
**Weneedtocreateatemptabletodothecalculation.
 
**reserved:sum(reserved)whereindidin(0,1,255)
 
**data:sum(dpages)whereindid<2+sum(used)whereindid=255(text)
 
**indexp:sum(used)whereindidin(0,1,255)-data
 
**unused:sum(reserved)-sum(used)whereindidin(0,1,255)
 
*/
 
declarecur_tablecursorfor
 
selectnamefromsysobjectswheretype='u'
 
Opencur_table
 
fetchnextfromcur_tableinto@objname
 
While@@FETCH_STATUS=0
 
begin
 
createtable#spt_space
 
(
 
rowsintnull,
 
reserveddec(15)null,
 
datadec(15)null,
 
indexpdec(15)null,
 
unuseddec(15)null
 
)
 
/*
 
**Checktoseeifuserwantsusagesupdated.
 
*/
 
if@updateusageisnotnull
 
begin
 
select@updateusage=lower(@updateusage)
 
if@updateusagenotin('true','false')
 
begin
 
raiserror(15143,-1,-1,@updateusage)
 
return(1)
 
end
 
end
 
/*
 
**Checktoseethattheobjnameislocal.
 
*/
 
if@objnameISNOTNULL
 
begin
 
select@dbname=parsename(@objname,3)
 
if@dbnameisnotnulland@dbname<>db_name()
 
begin
 
raiserror(15250,-1,-1)
 
return(1)
 
end
 
if@dbnameisnull
 
select@dbname=db_name()
 
/*
 
**Trytofindtheobject.
 
*/
 
select@id=null
 
select@id=id,@type=xtype
 
fromsysobjects
 
whereid=object_id(@objname)
 
/*
 
**Doestheobjectexist?
 
*/
 
if@idisnull
 
begin
 
raiserror(15009,-1,-1,@objname,@dbname)
 
return(1)
 
end
 
ifnotexists(select*fromsysindexes
 
where@id=idandindid<2)
 
if@typein('P','D','R','TR','C','RF')--datastoredinsysprocedures
 
begin
 
raiserror(15234,-1,-1)
 
return(1)
 
end
 
elseif@type='V'--View=>nophysicaldatastorage.
 
begin
 
raiserror(15235,-1,-1)
 
return(1)
 
end
 
elseif@typein('PK','UQ')--nophysicaldatastorage.--?!?!toomanysimilarmessages
 
begin
 
raiserror(15064,-1,-1)
 
return(1)
 
end
 
elseif@type='F'--FK=>nophysicaldatastorage.
 
begin
 
raiserror(15275,-1,-1)
 
return(1)
 
end
 
end
 
/*
 
**Updateusagesifuserspecifiedtodoso.
 
*/
 
if@updateusage='true'
 
begin
 
if@objnameisnull
 
dbccupdateusage(0)withno_infomsgs
 
else
 
dbccupdateusage(0,@objname)withno_infomsgs
 
print''
 
end
 
setnocounton
 
/*
 
**If@idisnull,thenwewantsummarydata.
 
*/
 
/*Spaceusedcalculatedinthefollowingway
 
**@dbsize=Pagesused
 
**@bytesperpage=d.low(whered=master.dbo.spt_values)is
 
**the#ofbytesperpagewhend.type='E'and
 
**d.number=1.
 
**Size=@dbsize*d.low/(1048576(OR1MB))
 
*/
 
if@idisnull
 
begin
 
select@dbsize=sum(convert(dec(15),size))
 
fromdbo.sysfiles
 
where(status&64=0)
 
select@logsize=sum(convert(dec(15),size))
 
fromdbo.sysfiles
 
where(status&64<>0)
 
select@bytesperpage=low
 
frommaster.dbo.spt_values
 
wherenumber=1
 
andtype='E'
 
select@pagesperMB=1048576/@bytesperpage
 
selectdatabase_name=db_name(),
 
database_size=
 
ltrim(str((@dbsize+@logsize)/@pagesperMB,15,2)+'MB'),
 
'unallocatedspace'=
 
ltrim(str((@dbsize-
 
(selectsum(convert(dec(15),reserved))
 
fromsysindexes
 
whereindidin(0,1,255)
 
))/@pagesperMB,15,2)+'MB')
 
print''
 
/*
 
**Nowcalculatethesummarydata.
 
**reserved:sum(reserved)whereindidin(0,1,255)
 
*/
 
insertinto#spt_space(reserved)
 
selectsum(convert(dec(15),reserved))
 
fromsysindexes
 
whereindidin(0,1,255)
 
/*
 
**data:sum(dpages)whereindid<2
 
**+sum(used)whereindid=255(text)
 
*/
 
select@pages=sum(convert(dec(15),dpages))
 
fromsysindexes
 
whereindid<2
 
select@pages=@pages+isnull(sum(convert(dec(15),used)),0)
 
fromsysindexes
 
whereindid=255
 
update#spt_space
 
setdata=@pages
 
/*index:sum(used)whereindidin(0,1,255)-data*/
 
update#spt_space
 
setindexp=(selectsum(convert(dec(15),used))
 
fromsysindexes
 
whereindidin(0,1,255))
 
-data
 
/*unused:sum(reserved)-sum(used)whereindidin(0,1,255)*/
 
update#spt_space
 
setunused=reserved
 
-(selectsum(convert(dec(15),used))
 
fromsysindexes
 
whereindidin(0,1,255))
 
selectreserved=ltrim(str(reserved*d.low/1024.,15,0)+
 
''+'KB'),
 
data=ltrim(str(data*d.low/1024.,15,0)+
 
''+'KB'),
 
index_size=ltrim(str(indexp*d.low/1024.,15,0)+
 
''+'KB'),
 
unused=ltrim(str(unused*d.low/1024.,15,0)+
 
''+'KB')
 
from#spt_space,master.dbo.spt_valuesd
 
whered.number=1
 
andd.type='E'
 
end
 
/*
 
**Wewantaparticularobject.
 
*/
 
else
 
begin
 
/*
 
**Nowcalculatethesummarydata.
 
**reserved:sum(reserved)whereindidin(0,1,255)
 
*/
 
insertinto#spt_space(reserved)
 
selectsum(reserved)
 
fromsysindexes
 
whereindidin(0,1,255)
 
andid=@id
 
/*
 
**data:sum(dpages)whereindid<2
 
**+sum(used)whereindid=255(text)
 
*/
 
select@pages=sum(dpages)
 
fromsysindexes
 
whereindid<2
 
andid=@id
 
select@pages=@pages+isnull(sum(used),0)
 
fromsysindexes
 
whereindid=255
 
andid=@id
 
update#spt_space
 
setdata=@pages
 
/*index:sum(used)whereindidin(0,1,255)-data*/
 
update#spt_space
 
setindexp=(selectsum(used)
 
fromsysindexes
 
whereindidin(0,1,255)
 
andid=@id)
 
-data
 
/*unused:sum(reserved)-sum(used)whereindidin(0,1,255)*/
 
update#spt_space
 
setunused=reserved
 
-(selectsum(used)
 
fromsysindexes
 
whereindidin(0,1,255)
 
andid=@id)
 
update#spt_space
 
setrows=i.rows
 
fromsysindexesi
 
wherei.indid<2
 
andi.id=@id
 
insertinto#temp1
 
selectname=object_name(@id),
 
rows=convert(char(11),rows),
 
reserved=ltrim(str(reserved*d.low/1024.,15,0)+
 
''+'KB'),
 
data=ltrim(str(data*d.low/1024.,15,0)+
 
''+'KB'),
 
index_size=ltrim(str(indexp*d.low/1024.,15,0)+
 
''+'KB'),
 
unused=ltrim(str(unused*d.low/1024.,15,0)+
 
''+'KB')
 
from#spt_space,master.dbo.spt_valuesd
 
whered.number=1
 
andd.type='E'
 
Droptable#spt_space
 
end
 
fetchnextfromcur_tableinto@objname
 
end
 
Closecur_table
 
DEALLOCATEcur_table
 
Select*from#temp1orderbylen(数据使用空间)desc,数据使用空间desc,保留空间desc
 
Droptable#temp1
 
return(0)
 
end
 
GO
</code>

注:关于一个查看SQL ServerServer数据库空间使用情况的存储过程 SpaceUsed的内容就先介绍到这里,更多相关文章的可以留意

代码注释

作者:喵哥笔记

IDC笔记

学的不仅是技术,更是梦想!