Call now: (800) 766-1884  



 Home


 SQL Server Tips
 SQL Server Training

 SQL Server Consulting
 SQL Server Support
 SQL Server Remote DBA



 Articles
 Services
 SQL Server Scripts
 Scripts Menu



 

 

 

 
 

SQL Server script to display files for autogrowth


SQL Server Tips by Burleson Consulting
 

*********************************************************
-- up_bn_storage_files
*********************************************************
From filegroups, the DBA can drill down to the individual file 
level with the up_bn_storage_files procedure in SQL Server 2000 
and above. This script will reveal whether any files are not 
enabled for autogrowth and if any file has a maximum file limit 
imposed. 

IF OBJECT_ID('dbo.up_bn_storage_files') IS NOT NULL
BEGIN
DROP PROCEDURE dbo.up_bn_storage_files
IF OBJECT_ID('dbo.up_bn_storage_files') IS NOT NULL
PRINT '<<< FAILED DROPPING PROCEDURE dbo.up_bn_storage_files >>>'
ELSE
PRINT '<<< DROPPED PROCEDURE dbo.up_bn_storage_files >>>'
END
go
CREATE PROCEDURE up_bn_storage_files
AS
set nocount on

declare @db_name sysname,
@testamt float

create table #file_info
(dbname sysname NULL,
groupid int NULL,
groupname sysname NULL,
logicname nchar(128) NULL,
filename nchar(260) NULL,
filestatus int NULL, /* to get data or log */
file_size float NULL,
file_max_size float NULL,
file_growth_size float NULL)

declare 
db_cursor cursor for
select 
name
from 
            master..sysdatabases
See code depot for full script
where 
databasepropertyex(name,'status') not in 
('SUSPECT', 'OFFLINE', 'RESTORING', 'RECOVERING') and 
has_dbaccess(name) = 1

open db_cursor
fetch 
   db_cursor 
into 
@db_name

while @@fetch_status = 0
begin
insert 
into #file_info
(dbname,
groupid,
groupname,
logicname,
filename,
    filestatus,
file_size,
file_max_size,
file_growth_size)
exec 
('use [' + @db_name + '] 
select 
db_name = db_name(),
b.groupid,
a.groupname,
b.name,
b.filename,
b.status,
b.size,
b.maxsize,
b.growth 
from 
sysfilegroups a, 
sysfiles b 
  where 
a.groupid =* b.groupid')

fetch 
db_cursor 
into 
@db_name
end

select 
dbname,
logicname,
file_group = case groupname
when NULL then 'LOG'
      else groupname
end,
filename,
size_in_mb = convert(decimal(17,2),((file_size * 8) / 1024)),
can_grow = case file_growth_size
when 0 then 'NO'
else 'YES'
end,
growth_amount = case (filestatus&0x100000)
when 0x100000 then convert(varchar(18),file_growth_size) + '%'
else convert(varchar(18),(file_growth_size * 8) / 1024) + ' MB'
end,
max_file_size_mb = case(file_max_size)
  when -1 then 'UNLIMITED'
else convert(varchar(18),((file_max_size * 8) / 1024))
end
from
#file_info
order by 
1

deallocate db_cursor
drop table #file_info
go
IF OBJECT_ID('up_bn_storage_files') IS NOT NULL
PRINT '<<< CREATED PROCEDURE up_bn_storage_files >>>'
ELSE
PRINT '<<< FAILED CREATING PROCEDURE up_bn_storage_files >>>'
go

< up_bn_storage_files (database cursor code segment)
declare db_cursor cursor for
select name
   from master..sysdatabases
where databaseproperty(name,N'IsShutdown') <> 1 and
databaseproperty(name,N'IsInRecovery') <> 1 and
databaseproperty(name,N'IsNotRecovered') <> 1 and 
databaseproperty(name,N'IsOffline') <> 1 and
databaseproperty(name,N'IsSuspect') <> 1 and 
has_dbaccess(name) = 1

 

 

 

Burleson Consulting Remote DB Administration


 

 


 

 

 

 

 
Burleson is the America's Team

Note: The pages on this site were created as a support and training reference for use by our staff of DBA consultants.  If you find it confusing, please exit this page.

Errata?  SQL Server technology is changing and we strive to update our SQL Server support information.  If you find an error or have a suggestion for improving our content, we would appreciate your feedback.  Just  e-mail:and include the URL for the page.
 


Burleson Consulting
SQL Server database support

 

Copyright 1996 -  2013 by Vaaltech Web Services. All rights reserved.

Hit Counter