这是我的数据库结构:
- 4 个文件组:PRIMARY、FileGroup201708、FileGroup201709、FileGroup201710
- 4 个文件:File1.mdf、File201708.ndf、File201709.ndf、File201710.ndf
这是分区方案和功能:
CREATE PARTITION FUNCTION [MyFunction](datetime)
AS RANGE RIGHT FOR VALUES (N'2017-08-01T00:00:00.000', N'2017-09-01T00:00:00.000', N'2017-10-01T00:00:00.000')
CREATE PARTITION SCHEME [MyScheme]
AS PARTITION [MyFunction] TO ([PRIMARY], [FileGroup201708], [FileGroup201709], [FileGroup201710])
数据库中的所有表都是分区的。
好吧,我决定删除 201708 期间的所有数据,首先我执行:
TRUNCATE TABLE MyTable WITH (PARTITIONS (partition_number))
对于所有表。此操作后,表中没有任何分区数据。
SELECT
p.partition_number, fg.name, p.rows, OBJECT_NAME(p.object_id)
FROM
sys.partitions p
INNER JOIN
sys.allocation_units au ON au.container_id = p.hobt_id
INNER JOIN
sys.filegroups fg ON fg.data_space_id = au.data_space_id
WHERE
p.object_id = OBJECT_ID('MyTable')
上面的查询给了我特定文件组的 0 行。
现在我想删除文件和文件组,当我尝试删除文件时没有问题,但是当我尝试删除文件组时,我从 SQL Server 收到错误消息:
无法删除文件组“FileGroup201708”,因为它不为空
我使用此查询检查了该文件组中是否有一些索引:
SELECT
OBJECT_NAME(i.[object_id]) AS [ObjectName],
i.[index_id] AS [IndexID],
i.[name] AS [IndexName],
i.[type_desc] AS [IndexType],
i.[data_space_id] AS [DatabaseSpaceID],
f.[name] AS [FileGroup],
d.[physical_name] AS [DatabaseFileName]
FROM
[sys].[indexes] i
INNER JOIN
[sys].[filegroups] f ON f.[data_space_id] = i.[data_space_id]
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.[object_id], 'IsUserTable') = 1
ORDER BY
OBJECT_NAME(i.[object_id]), f.[name], i.[data_space_id]
但是所有行都在“FileGroup”列中,PRIMARY 文件组。谁能给点建议?