Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


logical and physical file names of all databases


logical and physical file names of all databases

Author
Message
sqlfriends
sqlfriends
SSCrazy
SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)

Group: General Forum Members
Points: 2041 Visits: 3853
Is there a query or sp that I can pull all databases on the server along with their logical file names and physical file names?



Thanks
StormNorm
StormNorm
Old Hand
Old Hand (349 reputation)Old Hand (349 reputation)Old Hand (349 reputation)Old Hand (349 reputation)Old Hand (349 reputation)Old Hand (349 reputation)Old Hand (349 reputation)Old Hand (349 reputation)

Group: General Forum Members
Points: 349 Visits: 430
Try this code...


USE [master]
GO

CREATE PROCEDURE [dbo].[dba_rpt_SQL_Files_Report]

AS

/*

   By Norm Enger - 2007 - http://thesqlguy.wordpress.com/
   
   Reports on the file info for all dbs on a SQL instance, SQL 2000 and above
   
   EXEC dba_rpt_SQL_Files_Report
   
*/

SET NOCOUNT ON

DECLARE @cmd1 VARCHAR(8000)
DECLARE @cmd2 VARCHAR(8000)
DECLARE @cmd3 VARCHAR(8000)

CREATE TABLE #data(
   dbname VARCHAR(300),
   dbcompat VARCHAR(10),
   Fileid int NOT NULL,
   [FileGroup] int NOT NULL,
   TotalExtents int NOT NULL,
   UsedExtents int NOT NULL,
   [Name] sysname NOT NULL,
   [FileName] varchar(300) NOT NULL,
   FilePath VARCHAR(500) NULL,
   Autogrowth    VARCHAR(500) NULL,
   init_size_MB DECIMAL (38,7) NULL,
   tempdb_growth_MB DECIMAL (38,7) NULL
)

CREATE TABLE #log(
   id int identity(1,1) not null,
   groupid INT DEFAULT 0 NULL,
   dbname sysname NOT NULL,
   dbcompat VARCHAR(10),
--   LogSize numeric(15,7) NOT NULL,
   LogSize numeric(38,7) NOT NULL,
--   LogUsed numeric(9,5) NOT NULL,
   LogUsed numeric(38,6) NOT NULL,
   Status int NOT NULL,
   [Name] VARCHAR(300) NULL,
   FilePath VARCHAR(500) NULL,
   Autogrowth    VARCHAR(500) NULL,
   init_size_MB DECIMAL (38,7) NULL,
   tempdb_growth_MB DECIMAL (38,7) NULL
)

DECLARE @DBs table (name sysname, status varchar(100), dbcompat varchar(10))
DECLARE @CurrDB varchar(500)
DECLARE @CurrCmptlevel varchar(10)

INSERT INTO @DBs
(name, dbcompat)
SELECT name, cmptlevel
FROM sysdatabases
WHERE category IN ('0', '1','16')
AND CAST(DATABASEPROPERTYEX (name, 'status') AS VARCHAR(500)) = 'ONLINE'
ORDER BY name

SELECT @CurrDB = MIN(name) FROM @DBs
SELECT @CurrCmptlevel = dbcompat FROM @DBs WHERE name = @CurrDB

--Data files
WHILE @CurrDB IS NOT NULL
BEGIN
   SET @cmd1 = 'USE [' + @CurrDB + ']; INSERT #data (Fileid, FileGroup, TotalExtents, UsedExtents, Name, FileName) EXEC(''DBCC showfilestats with no_infomsgs''); UPDATE #data SET dbname = ''' + @CurrDB + ''', dbcompat = ''' + CAST(@CurrCmptlevel AS VARCHAR(10)) + ''' WHERE dbname IS NULL; update #data set [FilePath] = s.filename, Autogrowth = ''Autogrowth: '' + CASE WHEN (s.status & 0x100000 = 0 AND CEILING((s.growth * 8192.0)/(1024.0*1024.0)) = 0.00) OR s.growth = 0 THEN ''None'' WHEN s.status & 0x100000 = 0 THEN ''By '' + CONVERT(VARCHAR,CEILING((s.growth * 8192.0)/(1024.0*1024.0))) + '' MB'' ELSE ''By '' + CONVERT(VARCHAR, s.growth) + '' percent'' END + CASE WHEN (s.status & 0x100000 = 0 AND CEILING((s.growth * 8192.0)/(1024.0*1024.0)) = 0.00) OR s.growth = 0 THEN '''' WHEN CAST([maxsize]*8.0/1024 AS DEC(20,2)) <= 0.00 THEN '', unrestricted growth'' ELSE '', restricted growth to '' + CAST(CAST([maxsize]*8.0/1024 AS DEC(20)) AS VARCHAR) + '' MB'' END from #data d inner join dbo.sysfiles s on d.FileGroup = s.groupid and d.Fileid = s.fileid where d.dbname = ''' + @CurrDB + ''''
   
   EXEC (@cmd1)

   SELECT @CurrDB = MIN(name) FROM @DBs WHERE name > @CurrDB
   SELECT @CurrCmptlevel = dbcompat FROM @DBs WHERE name = @CurrDB

END

SELECT @CurrDB = MIN(name) FROM @DBs
SELECT @CurrCmptlevel = dbcompat FROM @DBs WHERE name = @CurrDB

--Log files
WHILE @CurrDB IS NOT NULL
BEGIN

   --Update log file logical names
   SET @cmd3 = 'USE [' + @CurrDB + ']; INSERT #log (dbname, LogSize, LogUsed, Status) EXEC(''DBCC sqlperf(logspace) with no_infomsgs''); update #log set [Name] = s.name, [FilePath] = s.filename, Autogrowth = ''Autogrowth: '' + CASE WHEN (s.status & 0x100000 = 0 AND CEILING((s.growth * 8192.0)/(1024.0*1024.0)) = 0.00) OR s.growth = 0 THEN ''None'' WHEN s.status & 0x100000 = 0 THEN ''By '' + CONVERT(VARCHAR,CEILING((s.growth * 8192.0)/(1024.0*1024.0))) + '' MB'' ELSE ''By '' + CONVERT(VARCHAR, s.growth) + '' percent'' END + CASE WHEN (s.status & 0x100000 = 0 AND CEILING((s.growth * 8192.0)/(1024.0*1024.0)) = 0.00) OR s.growth = 0 THEN '''' WHEN CAST([maxsize]*8.0/1024 AS DEC(20,2)) <= 0.00 THEN '', unrestricted growth'' ELSE '', restricted growth to '' + CAST(CAST([maxsize]*8.0/1024 AS DEC(20)) AS VARCHAR) + '' MB'' END from #log l inner join dbo.sysfiles s on l.groupid = s.groupid where l.dbname = ''' + @CurrDB + '''; update #log set dbcompat = ''' + @CurrCmptlevel + ''' where dbname = ''' + @CurrDB + ''''

   EXEC (@cmd3)

   SELECT @CurrDB = MIN(name) FROM @DBs WHERE name > @CurrDB
   SELECT @CurrCmptlevel = dbcompat FROM @DBs WHERE name = @CurrDB

END

--Clean up #log
DELETE #log where Name IS NULL

DELETE #log FROM #log l
lEFT JOIN
(
SELECT dbname, Name, MIN(id) AS id FROM #log GROUP BY dbname, Name
) k
ON l.dbname = k.dbname AND l.Name = k.Name AND l.id = k.id
WHERE k.id IS NULL

--Get tempdb initial file sizes - update tempdb data files init sizes
UPDATE [#data]
SET [init_size_MB] = a.Initial_Size_MB
FROM #data d
INNER JOIN
(
   SELECT
      [DB_Name] = 'tempdb',
      f.[name] AS logical_file_Name,
      (CAST(f.[size] AS DECIMAL) * 8192) / 1024 /1024 AS Initial_Size_MB
   FROM master..sysaltfiles f
      INNER JOIN master..sysdatabases d
      ON f.dbid = d.dbid
   WHERE d.name = 'tempdb') AS a
ON d.dbname = a.[DB_Name]
AND d.[Name] = a.logical_file_Name

--Get tempdb initial file sizes - update tempdb log file init sizes
UPDATE [#log]
SET [init_size_MB] = a.Initial_Size_MB
FROM #log l
INNER JOIN
(
   SELECT
      [DB_Name] = 'tempdb',
      f.[name] AS logical_file_Name,
      (CAST(f.[size] AS DECIMAL) * 8192) / 1024 /1024 AS Initial_Size_MB
   FROM master..sysaltfiles f
      INNER JOIN master..sysdatabases d
      ON f.dbid = d.dbid
   WHERE d.name = 'tempdb') AS a
ON l.dbname = a.[DB_Name]
AND l.[Name] = a.logical_file_Name

--Calculate tempdb files growth
UPDATE [#data]
SET [tempdb_growth_MB] =
CASE
   WHEN ((TotalExtents*64)/1024.0) - init_size_MB < 0.00 THEN 0.00
   ELSE ((TotalExtents*64)/1024.0) - init_size_MB
END

UPDATE [#log]
SET [tempdb_growth_MB] =
CASE
   WHEN LogSize - init_size_MB < 0.00 THEN 0.00
   ELSE LogSize - init_size_MB
END

--Return results for all dbs
SELECT
   dbname + ' (' + CAST(DATABASEPROPERTYEX (dbname, 'recovery') AS VARCHAR(500)) + ') (' + dbcompat + ')' AS [DB_NAME],
   dbname AS DB_NAME_2,
   'Data File' AS [Type],
   [Name] AS [NAME],
   [FilePath],
   init_size_MB,
   (TotalExtents*64)/1024.0 AS [TotalMB],
   tempdb_growth_MB,
   (UsedExtents*64)/1024.0 AS [UsedMB],
   (TotalExtents*64)/1024.0 - (UsedExtents*64)/1024.0 AS [FreeMB],
   [Autogrowth]
FROM #data
UNION
SELECT
   dbname + ' (' + CAST(DATABASEPROPERTYEX (dbname, 'recovery') AS VARCHAR(500)) + ') (' + dbcompat + ')' AS [DB_NAME],
   dbname AS DB_NAME_2,   
   'TLog File',
   [Name] AS [NAME],
   [FilePath],
   init_size_MB,
   LogSize,
   tempdb_growth_MB,
   ((LogUsed/100)*LogSize),
   LogSize - ((LogUsed/100)*LogSize),
   [Autogrowth]
FROM #log
ORDER BY 1, [Type], [Name]

DROP TABLE #data
DROP TABLE #log

GO





GilaMonster
GilaMonster
SSC-Forever
SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)

Group: General Forum Members
Points: 47187 Visits: 44356
Sure, easy query.

SELECT DB_NAME(database_id) AS DatabaseName, name AS LogicalFileName, physical_name AS PhysicalFileName 
FROM sys.master_files AS mf




Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


sqlfriends
sqlfriends
SSCrazy
SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)

Group: General Forum Members
Points: 2041 Visits: 3853
Thanks, the easy query works better.
simon.sarjeant
simon.sarjeant
Forum Newbie
Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)

Group: General Forum Members
Points: 3 Visits: 73
How about SELECT *
FROM [master].[dbo].[sysaltfiles] for SQL Server 2000?

Easy!
vishe.dinesh
vishe.dinesh
Forum Newbie
Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)

Group: General Forum Members
Points: 8 Visits: 185
SQLCMD -S . -d Operdb -E -Q “SELECT DB_NAME(database_id) AS DatabaseName, name AS LogicalFileName, physical_name AS PhysicalFileName
FROM sys.master_files AS mf” -s “,” -o “C:\result.csv”


it come outside from command prompt.. how to use ?
lesemv
lesemv
Forum Newbie
Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)

Group: General Forum Members
Points: 3 Visits: 2
I deleted my database via Management Studio and was attempting to re-create it with the same name and I get the "logical filename <...> is already in use". I looked at sys.master_files and didn't see any remnants, but there must be somewhere. How do I clean this up, so I can use the same name? Thanks.
lesemv
lesemv
Forum Newbie
Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)

Group: General Forum Members
Points: 3 Visits: 2
nevermind
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)

Group: General Forum Members
Points: 44994 Visits: 39880
lesemv (3/15/2016)
nevermind


What did you find to make you say "nevermind"?

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
     Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
lesemv
lesemv
Forum Newbie
Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)

Group: General Forum Members
Points: 3 Visits: 2
I'm embarrassed to say. I had the same 'Name' value for the data and log file.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search