使用 TSQL 查找 SQL 2008 上文件组的物理位置

问题描述 投票:0回答:3

如何使用 tsql 查找 SQL Server 2008 上文件组的物理位置?

sql-server-2008 t-sql
3个回答
6
投票

试试这个..

SELECT * FROM sys.database_files

SELECT * FROM sys.sysaltfiles

还有
使用此脚本可以查看对象及其实际物理文件名和位置的更详细图片。

SELECT    OBJECT_NAME(i.id) AS Table_Name
        , i.indid           AS Index_ID
        , i.name            AS Index_Name
        , i.groupid         AS Group_ID
        , f.name            AS File_Group
        , d.physical_name   AS [File_Name]
        , s.name            AS Dataspace
FROM        sys.sysindexes      i
INNER JOIN  sys.filegroups      f   ON f.data_space_id = i.groupid
INNER JOIN  sys.database_files  d   ON f.data_space_id = d.data_space_id
INNER JOIN  sys.data_spaces     s   ON f.data_space_id = s.data_space_id
WHERE   OBJECTPROPERTY(i.id, 'IsUserTable') = 1

1
投票

这可能就是您想看到的:

DECLARE @db sysname;
DECLARE @command nvarchar(4000)
DECLARE @cc nvarchar(4000)

SELECT @cc = N'
SELECT
[db] = DB_NAME(),
[fg_name] = fg.[name],
[db_file_name] = df.[name],
[db_file_path] = df.[physical_name], 
[file_size_(gb)] = CAST(ROUND(df.[size]/128.0/1024.0,3) as decimal(19,3)),
[fg_type] = fg.[type_desc],
[is_fg_read_only] = fg.[is_read_only]
FROM sys.filegroups fg
JOIN sys.database_files df ON df.[data_space_id] = fg.[data_space_id];'

IF OBJECT_ID('tempdb.dbo.#fg', 'U') IS NOT NULL DROP TABLE #fg;  
CREATE TABLE #fg
(
[db] sysname NOT NULL,
[fg_name] sysname NOT NULL,
[db_file_name] sysname NOT NULL,
[db_file_path] nvarchar(260) NOT NULL,
[file_size_(gb)] decimal(19,3),
[fg_type] nvarchar(60) NOT NULL,
[is_fg_read_only] bit NOT NULL
);

DECLARE database_cursor CURSOR FOR 
SELECT [name]
FROM master.sys.databases 
WHERE [state] = 0
AND [is_in_standby] = 0;
OPEN database_cursor

FETCH NEXT FROM database_cursor INTO @db 

WHILE @@FETCH_STATUS = 0 
BEGIN 
    SET @command = 'USE ' + QUOTENAME(@db) + @cc
    INSERT INTO #fg EXEC sp_executesql @command 
    FETCH NEXT FROM database_cursor INTO @db
END 

CLOSE database_cursor 
DEALLOCATE database_cursor

SELECT * 
FROM #fg
ORDER BY [db] ASC, [fg_name] ASC;

enter image description here


0
投票

我认为sys.data_spaces返回的信息实际上是数据的逻辑位置而不是物理位置。

sys.data_spaces 和分区

© www.soinside.com 2019 - 2024. All rights reserved.