从数据库中删除文件组和文件

时间:2017-09-06 15:29:00

标签: sql-server sql-server-2016 database-partitioning filegroup

这是我的数据库结构:

  • 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]

但是所有行都在列#34; FileGroup",PRIMARY文件组中。任何人都可以提出一些建议吗?

3 个答案:

答案 0 :(得分:0)

我遇到了同样的问题(无法删除文件组)。 我所做的是:

  1. DROP TABLE MyTable

  2. 这些文件组中的
  3. DROP文件

    ALTER DATABASE MyDB REMOVE FILE logical_file_name_found_in_db_properties_in_files_section

  4. DROP分区计划MyScheme

  5. 拖放分区功能MyFunction

  6. DROP文件组

答案 1 :(得分:0)

这是我的解决方案:

ALTER TABLE Fisier DROP column Fisier
GO
ALTER TABLE Fisier SET (FILESTREAM_ON="NULL")
GO
ALTER TABLE FisierRev DROP column Fisier
GO
ALTER TABLE FisierRev SET (FILESTREAM_ON="NULL")
GO
ALTER TABLE Contract DROP column Fisier
GO
ALTER TABLE Contract SET (FILESTREAM_ON="NULL")
GO

USE gestiunea_inan_19Rebut
GO
DBCC SHRINKFILE (N'inan_magazin5_fs', EMPTYFILE)
GO


EXEC sp_filestream_force_garbage_collection @dbname = N'gestiunea_inan_19Rebut'

ALTER Database gestiunea_inan_19Rebut REMOVE FILE inan_magazin5_fs
GO
ALTER Database gestiunea_inan_19Rebut REMOVE FILEGROUP FILESTREAMGROUP
GO


IF COL_LENGTH('Fisier','Fisier') IS NULL
BEGIN
    ALTER TABLE Fisier ADD Fisier [varbinary](max)   NULL
END
GO
IF COL_LENGTH('FisierRev','Fisier') IS NULL
BEGIN
    ALTER TABLE FisierRev ADD Fisier [varbinary](max)   NULL
END
GO

IF COL_LENGTH('Contract','Fisier') IS NULL
BEGIN
    ALTER TABLE Contract ADD Fisier [varbinary](max)   NULL
END
GO

答案 2 :(得分:0)

试试

GO
ALTER PARTITION FUNCTION MyFunction() MERGE RANGE (31);
GO
ALTER DATABASE [MYDB] REMOVE FILE [File31]
GO
ALTER DATABASE [MYDB] REMOVE FILEGROUP [FileGroup_31]
GO