获取所有 FILESTREAM 数据的物理位置的 SQL 查询(源)
SELECT t.name AS 'table', c.name AS 'column', fg.name AS 'filegroup_name', dbf.type_desc AS 'type_description', dbf.physical_name AS 'physical_location' FROM sys.filegroups fg INNER JOIN sys.database_files dbf ON fg.data_space_id = dbf.data_space_id INNER JOIN sys.tables t ON fg.data_space_id = t.filestream_data_space_id INNER JOIN sys.columns c ON t.object_id = c.object_id AND c.is_filestream = 1
所有 FILESTREAM BLOB-fields 查询结果示例
获取服务器上 FILESTREAM 数据的子文件夹的 SQL 查询:(
这些表仅在专用管理员连接 (DAC) 中使用)。
SELECT o.name AS [Table], cp.name AS [Column], r.rsguid AS [Rowset GUID], rs.colguid AS [Column GUID] FROM SYS.SYSROWSETS r CROSS APPLY sys.sysrscols rs JOIN sys.partitions p ON rs.rsid = p.partition_id JOIN sys.objects o ON o.object_id = p.object_id JOIN sys.syscolpars cp ON cp.colid = rs.rscolid WHERE rs.colguid 不是 NULL 并且 o.object_id = cp.id AND r.rsguid 不是 NULL 并且 r.rowsetid = rs.rsid AND o.name = 'DOCUMENT' 和 cp.name = 'DIGITAL_FILE';
2.1。查询结果:
表:文档
列: DIGITAL_FILE
行集 GUID: 0x6AA5E6045794D34D8B1FAC0F49A49B0A
列 GUID: 0xD756E638FB2CC843AE98F489B57F6D7D
从此指南计算子路径:
0x6AA5E6045794D34D8B1FAC0F49A49B0A 等于此路径:04e6a56a-9457-4dd3-8b1f-ac0f49a49b0a
【反转6AA5E604】-【反转5794】-【反转D34D】-【反转8B1F】-【原装AC0F49A49B0A】
0xD756E638FB2CC843AE98F489B57F6D7D 等于此路径:38e656d7-2cfb-43c8-ae98-f489b57f6d7d(previus guid 解析中的规则)
2.2 结果计算出FILESTREAM存储的完整路径:
i:\SQL Base posc_astrachan FileStreams\GTMK\GTM_FILE_STREAM\04e6a56a-9457-4dd3-8b1f-ac0f49a49b0a\38e656d7-2cfb-43c8-ae98-f489b57f6d7d
- 获取 NTFS 文件夹中 BLOB 值的原始文件名。
3.1。用于查询高级 SQL Server 页面信息的存储过程
SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[procDBCC_PAGE]
@db_name varchar (500),
@filenum INT,
@pagenum INT
AS
BEGIN
SET NOCOUNT ON
DBCC TRACEON (3604);
DBCC PAGE (@db_name, @filenum, @pagenum, 3) WITH TABLERESULTS;
SET NOCOUNT OFF
END
3.2. 用于查询表的 FILESTREAM 的 BLOB 字段的原始文件名的存储过程
SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[procFindLogSequenceNumber]
-- @TableName varchar (500),
@instanceS varchar (19), -- key value for filed INSTANCE_S
@tableName varchar(500), -- DOCUMENT
@keyFieldName varchar(500), -- INSTANCE_S
@LogSequenceNumber varchar (500) OUTPUT
AS
SET NOCOUNT ON
DECLARE @db_name varchar (500)
DECLARE @filenum INT
DECLARE @pagenum INT
DECLARE @slotnum INT
DECLARE @rid varchar (100)
DECLARE @ridDotted varchar (100)
DECLARE @parent_object varchar (500)
DECLARE @sql nvarchar(2000)
DECLARE @sqlTable Table(physloc varchar(100))
DECLARE @DBCC_PAGE_Output Table ([ParentObject] varchar (MAX), [Object] varchar (MAX), [Field] varchar (MAX), [VALUE] varchar (MAX))
SET @db_name = db_name()
SET @sql = 'SELECT top 1 sys.fn_PhysLocFormatter (%%physloc%%) AS [PhysicalRID] FROM '+@tableName+' WHERE '
+@keyFieldName+' = '''+@instanceS+''''
INSERT @sqlTable (physloc)
EXECUTE sp_executesql @sql
SET @rid = (select top 1 physloc from @sqlTable)
if @rid is NULL
BEGIN
RETURN -1;
END
-- parse (@rid): (1:1172779:6) 1-@filenum, 2- @pagenum, 3- @slotnum
SET @ridDotted = Replace(@rid, ':', '.');
SET @ridDotted = Replace(@ridDotted, '(', '');
SET @ridDotted = Replace(@ridDotted, ')', '');
SET @filenum = (SELECT Parsename(@ridDotted, 3))
SET @pagenum = (SELECT Parsename(@ridDotted, 2))
SET @slotnum = (SELECT Parsename(@ridDotted, 1))
INSERT @DBCC_PAGE_Output ([ParentObject], [Object], [Field], [VALUE])
EXECUTE procDBCC_PAGE @db_name, @filenum , @pagenum
SET @parent_object = (SELECT TOP 1 [ParentObject] FROM @DBCC_PAGE_Output WHERE [Field] = 'INSTANCE_S'
AND [VALUE] = @instanceS)
--CreateLSN field Only
SET @LogSequenceNumber = (SELECT [VALUE] FROM @DBCC_PAGE_Output WHERE
[ParentObject] = @parent_object AND
[Field] = 'CreateLSN'
)
if @LogSequenceNumber is NULL
BEGIN
RETURN -1;
END
-- result 0006c050:00000120:0090 (442448:288:144)
-- clear (...)
SET @LogSequenceNumber = Replace(@LogSequenceNumber, ' ', '.');
SET @LogSequenceNumber = (SELECT Parsename(@LogSequenceNumber, 2))
--replace ":" to "-"
SET @LogSequenceNumber = Replace(@LogSequenceNumber, ':', '-');
SET NOCOUNT OFF
3.3. 用于获取 BLOB 的 NTFS 文件夹上的文件名的存储过程的示例查询:
declare @filestreamFileName varchar(500);
exec procFindLogSequenceNumber 'ZW_NU9hGZ0CKoSXYAoc', 'DOCUMENT', 'INSTANCE_S', @filestreamFileName OUTPUT
select @filestreamFileName
3.4. 结果(NTFS 文件夹中的原始文件名):
0003137a-00001244-00d0
3.5. 结果完整路径:
i:\SQL Base posc_astrachan FileStreams\GTMK\GTM_FILE_STREAM\04e6a56a-9457-4dd3-8b1f-ac0f49a49b0a\38e656d7-2cfb-43c8-ae98-f489b57f6d7d\0003137a-00001244-00d0