dfg

by sdf on December 29th, 2009
No notes
Syntax: No syntax
Show lines - Hide lines - Show in textbox - Download
  1.  
  2. /****** Object: StoredProcedure [dbo].[GetDatabaseSize] Script Date: 12/29/2009 17:00:29 ******/
  3. SET ANSI_NULLS ON
  4. GO
  5.  
  6. SET QUOTED_IDENTIFIER ON
  7. GO
  8.  
  9. CREATE PROCEDURE [dbo].[GetDatabaseSize]
  10. AS
  11. BEGIN
  12.  
  13. --tablesizes
  14. set nocount on;
  15.  
  16. set transaction isolation level read uncommitted;
  17.  
  18.  
  19. print 'Show Size, Space Used, Unused Space, Type, and Name of all database files'
  20.  
  21. select
  22. [FileSizeMB] =
  23. convert(numeric(10,2),sum(round(a.size/128.,2))),
  24. [UsedSpaceMB] =
  25. convert(numeric(10,2),sum(round(fileproperty( a.name,'SpaceUsed')/128.,2))) ,
  26. [UnusedSpaceMB] =
  27. convert(numeric(10,2),sum(round((a.size-fileproperty( a.name,'SpaceUsed'))/128.,2))) ,
  28. [Type] =
  29. case when a.groupid is null then '' when a.groupid = 0 then 'Log' else 'Data' end,
  30. [DBFileName] = isnull(a.name,'*** Total for all files ***')
  31. from
  32. sysfiles a
  33. group by
  34. groupid,
  35. a.name
  36. with rollup
  37. having
  38. a.groupid is null or
  39. a.name is not null
  40. order by
  41. case when a.groupid is null then 99 when a.groupid = 0 then 0 else 1 end,
  42. a.groupid,
  43. case when a.name is null then 99 else 0 end,
  44. a.name
  45.  
  46.  
  47.  
  48.  
  49. create table #TABLE_SPACE_WORK
  50. (
  51. TABLE_NAME sysname not null ,
  52. TABLE_ROWS numeric(18,0) not null ,
  53. RESERVED varchar(50) not null ,
  54. DATA varchar(50) not null ,
  55. INDEX_SIZE varchar(50) not null ,
  56. UNUSED varchar(50) not null ,
  57. )
  58.  
  59. create table #TABLE_SPACE_USED
  60. (
  61. Seq int not null
  62. identity(1,1) primary key clustered,
  63. TABLE_NAME sysname not null ,
  64. TABLE_ROWS numeric(18,0) not null ,
  65. RESERVED varchar(50) not null ,
  66. DATA varchar(50) not null ,
  67. INDEX_SIZE varchar(50) not null ,
  68. UNUSED varchar(50) not null ,
  69. )
  70.  
  71. create table #TABLE_SPACE
  72. (
  73. Seq int not null
  74. identity(1,1) primary key clustered,
  75. TABLE_NAME SYSNAME not null ,
  76. TABLE_ROWS int not null ,
  77. RESERVED int not null ,
  78. DATA int not null ,
  79. INDEX_SIZE int not null ,
  80. UNUSED int not null ,
  81. USED_MB numeric(18,4) not null,
  82. USED_GB numeric(18,4) not null,
  83. AVERAGE_BYTES_PER_ROW numeric(18,5) null,
  84. AVERAGE_DATA_BYTES_PER_ROW numeric(18,5) null,
  85. AVERAGE_INDEX_BYTES_PER_ROW numeric(18,5) null,
  86. AVERAGE_UNUSED_BYTES_PER_ROW numeric(18,5) null,
  87. )
  88.  
  89. declare @fetch_status int
  90.  
  91. declare @proc varchar(200)
  92. select @proc = rtrim(db_name())+'.dbo.sp_spaceused'
  93.  
  94. declare Cur_Cursor cursor local
  95. for
  96. select
  97. TABLE_NAME =
  98. rtrim(TABLE_SCHEMA)+'.'+rtrim(TABLE_NAME)
  99. from
  100. INFORMATION_SCHEMA.TABLES
  101. where
  102. TABLE_TYPE = 'BASE TABLE'
  103. order by
  104. 1
  105.  
  106. open Cur_Cursor
  107.  
  108. declare @TABLE_NAME varchar(200)
  109.  
  110. select @fetch_status = 0
  111.  
  112. while @fetch_status = 0
  113. begin
  114.  
  115. fetch next from Cur_Cursor
  116. into
  117. @TABLE_NAME
  118.  
  119. select @fetch_status = @@fetch_status
  120.  
  121. if @fetch_status <> 0
  122. begin
  123. continue
  124. end
  125.  
  126. truncate table #TABLE_SPACE_WORK
  127.  
  128. insert into #TABLE_SPACE_WORK
  129. (
  130. TABLE_NAME,
  131. TABLE_ROWS,
  132. RESERVED,
  133. DATA,
  134. INDEX_SIZE,
  135. UNUSED
  136. )
  137. exec @proc @objname =
  138. @TABLE_NAME ,@updateusage = 'true'
  139.  
  140.  
  141. -- Needed to work with SQL 7
  142. update #TABLE_SPACE_WORK
  143. set
  144. TABLE_NAME = @TABLE_NAME
  145.  
  146. insert into #TABLE_SPACE_USED
  147. (
  148. TABLE_NAME,
  149. TABLE_ROWS,
  150. RESERVED,
  151. DATA,
  152. INDEX_SIZE,
  153. UNUSED
  154. )
  155. select
  156. TABLE_NAME,
  157. TABLE_ROWS,
  158. RESERVED,
  159. DATA,
  160. INDEX_SIZE,
  161. UNUSED
  162. from
  163. #TABLE_SPACE_WORK
  164.  
  165. end --While end
  166.  
  167. close Cur_Cursor
  168.  
  169. deallocate Cur_Cursor
  170.  
  171. insert into #TABLE_SPACE
  172. (
  173. TABLE_NAME,
  174. TABLE_ROWS,
  175. RESERVED,
  176. DATA,
  177. INDEX_SIZE,
  178. UNUSED,
  179. USED_MB,
  180. USED_GB,
  181. AVERAGE_BYTES_PER_ROW,
  182. AVERAGE_DATA_BYTES_PER_ROW,
  183. AVERAGE_INDEX_BYTES_PER_ROW,
  184. AVERAGE_UNUSED_BYTES_PER_ROW
  185.  
  186. )
  187. select
  188. TABLE_NAME,
  189. TABLE_ROWS,
  190. RESERVED,
  191. DATA,
  192. INDEX_SIZE,
  193. UNUSED,
  194. USED_MB =
  195. round(convert(numeric(25,10),RESERVED)/
  196. convert(numeric(25,10),1024),4),
  197. USED_GB =
  198. round(convert(numeric(25,10),RESERVED)/
  199. convert(numeric(25,10),1024*1024),4),
  200. AVERAGE_BYTES_PER_ROW =
  201. case
  202. when TABLE_ROWS <> 0
  203. then round(
  204. (1024.000000*convert(numeric(25,10),RESERVED))/
  205. convert(numeric(25,10),TABLE_ROWS),5)
  206. else null
  207. end,
  208. AVERAGE_DATA_BYTES_PER_ROW =
  209. case
  210. when TABLE_ROWS <> 0
  211. then round(
  212. (1024.000000*convert(numeric(25,10),DATA))/
  213. convert(numeric(25,10),TABLE_ROWS),5)
  214. else null
  215. end,
  216. AVERAGE_INDEX_BYTES_PER_ROW =
  217. case
  218. when TABLE_ROWS <> 0
  219. then round(
  220. (1024.000000*convert(numeric(25,10),INDEX_SIZE))/
  221. convert(numeric(25,10),TABLE_ROWS),5)
  222. else null
  223. end,
  224. AVERAGE_UNUSED_BYTES_PER_ROW =
  225. case
  226. when TABLE_ROWS <> 0
  227. then round(
  228. (1024.000000*convert(numeric(25,10),UNUSED))/
  229. convert(numeric(25,10),TABLE_ROWS),5)
  230. else null
  231. end
  232. from
  233. (
  234. select
  235. TABLE_NAME,
  236. TABLE_ROWS,
  237. RESERVED =
  238. convert(int,rtrim(replace(RESERVED,'KB',''))),
  239. DATA =
  240. convert(int,rtrim(replace(DATA,'KB',''))),
  241. INDEX_SIZE =
  242. convert(int,rtrim(replace(INDEX_SIZE,'KB',''))),
  243. UNUSED =
  244. convert(int,rtrim(replace(UNUSED,'KB','')))
  245. from
  246. #TABLE_SPACE_USED aa
  247. ) a
  248. order by
  249. TABLE_NAME
  250.  
  251. print 'Show results in descending order by size in MB'
  252.  
  253. select * from #TABLE_SPACE order by TABLE_NAME asc;
  254.  
  255. drop table #TABLE_SPACE_WORK
  256. drop table #TABLE_SPACE_USED
  257. drop table #TABLE_SPACE
  258.  
  259. END
  260.  
  261. GO
  262.  
  263.  

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS