开发者

Retrieve file list of an SQL server database which is offline

开发者 https://www.devze.com 2023-01-17 10:53 出处:网络
I have some offline databases on a SQL server. I would like to know which files on disc are related to these databases. Is it possible to retrieve the file list of offline databases开发者_高级运维 wit

I have some offline databases on a SQL server. I would like to know which files on disc are related to these databases. Is it possible to retrieve the file list of offline databases开发者_高级运维 without taking them online first?


This will give you a list of all physical file paths related to any offline databases, along with database name and file type:

SELECT
'DB_NAME' = db.name,
'FILE_NAME' = mf.name,
'FILE_TYPE' = mf.type_desc,
'FILE_PATH' = mf.physical_name
FROM
sys.databases db
INNER JOIN sys.master_files mf
ON db.database_id = mf.database_id
WHERE
db.state = 6 -- OFFLINE


Or simply

select * from sys.databases where state_desc='OFFLINE'


List all the available, but offline SQL server database files

The following statement will list all the files associated with offline SQL server databases

SELECT
    m.physical_name + '\' + m.name AS [file_path]
FROM
    sys.databases AS d 
    INNER JOIN sys.master_files AS m ON d.database_id = m.database_id
WHERE
    d.state_desc = 'OFFLINE'
    --AND m.type_desc = 'ROWS'
GROUP BY
    m.physical_name + '\' + m.name

Note: Uncomment the line AND m.type_desc = 'ROWS' (delete the --) to filter the list further to include only database files. Otherwise, log files will also be listed.

The GROUP BY clause is there to prevent entries appearing more than once.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号