Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Code not quite working?

Author  Topic 

Bill_C
Constraint Violating Yak Guru

299 Posts

Posted - 2009-08-06 : 07:41:26
I am trying to get information on all the file sizes held in sysfiles, I have the following code but am pulling out information for only the database i am currently logged into.
I want to scroll through all the databases (except those in the exclude list) and get all the info for each one and put them in the table #tmp1.
How do i switch databases to get it working?




DECLARE @dname VARCHAR(50) --database name

CREATE TABLE #tmp1
(
[name] VARCHAR(50),
[fileid] INT,
[filename] VARCHAR (150),
[filegroup] VARCHAR(50),
[size] VARCHAR(50),
[maxsize] VARCHAR(50),
[growth] VARCHAR(50),
[useage] VARCHAR (30)
)

DECLARE db_cursor CURSOR FOR

SELECT [name] FROM sys.databases WHERE [name] NOT IN ('master', 'msdb', 'model')
OPEN db_cursor
FETCH NEXT FROM db_cursor INTO @dname
WHILE @@FETCH_STATUS = 0
BEGIN

INSERT INTO #tmp1

select name, fileid, filename,
filegroup = filegroup_name(groupid),
'size' = convert(nvarchar(15), convert (bigint, size) * 8) + N' KB',
'maxsize' = (case maxsize when -1 then N'Unlimited'
else
convert(nvarchar(15), convert (bigint, maxsize) * 8) + N' KB' end),
'growth' = (case status & 0x100000 when 0x100000 then
convert(nvarchar(15), growth) + N'%'
else
convert(nvarchar(15), convert (bigint, growth) * 8) + N' KB' end),
'usage' = (case status & 0x40 when 0x40 then 'log only' else 'data only' end)
from sysfiles
order by fileid
FETCH NEXT FROM db_cursor INTO @dname
END

CLOSE db_cursor
DEALLOCATE db_cursor
SELECT * FROM #tmp1


DROP TABLE #tmp1

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-08-06 : 07:53:33
select * from sys.master_files



N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

rajdaksha
Aged Yak Warrior

595 Posts

Posted - 2009-08-06 : 08:00:47
quote:

select * from sys.master_files




When you drop or rebuild large indexes, or drop or truncate large tables, the Database Engine defers the actual page deallocations, and their associated locks, until after the transaction commits. Deferred drop operations do not release allocated space immediately. Therefore, the values returned by sys.master_files immediately after dropping or truncating a large object may not reflect the actual disk space available.





-------------------------
R..
http://code.msdn.microsoft.com/SQLExamples/
http://msdn.microsoft.com/hi-in/library/bb500155(en-us).aspx
Go to Top of Page

Bill_C
Constraint Violating Yak Guru

299 Posts

Posted - 2009-08-06 : 08:03:58
Many Thanks, did not know about sys.master_files
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-08-06 : 08:15:59
Oh, I just learned the master_files is the configured and initial size for the the database files.
Are you using SQL Server 2000 or SQL Server 2005?



N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

Bill_C
Constraint Violating Yak Guru

299 Posts

Posted - 2009-08-06 : 08:51:10
2005
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-08-06 : 09:27:56
See this
CREATE TABLE	#Stage
(
dbName NVARCHAR(128),
fileID INT,
[fileName] NVARCHAR (128),
fileGroup NVARCHAR(128),
size NVARCHAR(128),
maxSize NVARCHAR(128),
growth NVARCHAR(128),
usage NVARCHAR (128)
)

INSERT #Stage
(
dbName,
fileID,
[fileName],
fileGroup,
size,
maxSize,
growth,
usage
)
SELECT mf.name AS dbName,
mf.[file_id] AS fileID,
mf.physical_name AS fileName,
ds.name AS fileGroup,
CONVERT(NVARCHAR(128), CONVERT(BIGINT, mf.size) * 8) + N' KB' AS size,
CASE mf.max_size
WHEN -1 THEN N'Unlimited'
ELSE CONVERT(NVARCHAR(128), CONVERT (BIGINT, mf.max_size) * 8) + N' KB'
END AS maxSize,
CASE mf.is_percent_growth
WHEN 1 THEN CONVERT(NVARCHAR(128), mf.growth) + N'%'
ELSE CONVERT(NVARCHAR(128), CONVERT (BIGINT, mf.growth) * 8) + N' KB'
END AS growth,
CASE mf.data_space_id
WHEN 0 THEN 'log only'
ELSE 'data only'
END AS Usage
FROM master.sys.master_files AS mf
LEFT JOIN master.sys.data_spaces AS ds ON ds.data_space_id = mf.data_space_id
WHERE mf.name NOT IN ('master', 'mastlog', 'modeldev', 'modellog', 'msdbdata', 'msdblog', 'tempdev', 'templog', 'distribution', 'distribution_log')

SELECT *
FROM #Stage

DROP TABLE #Stage



N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

Bill_C
Constraint Violating Yak Guru

299 Posts

Posted - 2009-08-06 : 09:34:30
Thanks Peso, works a treat
Go to Top of Page
   

- Advertisement -