简体   繁体   English

无法删除FileStream FileGroup或将其重新附加到SQL Server数据库

[英]Unable to remove or re-attach FileStream FileGroup to a SQL Server Database

Background: I had a working SQL Server Database created in SQL 2010, with 2 different FileStream/FileGroups, working fine. 背景:我在SQL 2010中创建了一个正常工作的SQL Server数据库,带有2个不同的FileStream / FileGroups,工作正常。 I detached the database, moved it to another desktop with SQL 2016 (on Win 10 Pro), and manually copied the 2 Filestream folders (called ImageFS and DocFS ). 我分离了数据库,使用SQL 2016(在Win 10 Pro上)将其移动到另一个桌面,然后手动复制了2个Filestream文件夹(称为ImageFS和DocFS )。 I simply re-attached the mdf database and didn't pay attention to the Filestream functions because it wasn't being used. 我只是简单地重新连接了mdf数据库,并且没有注意Filestream函数,因为它没有被使用。 Months later I noticed that one FileGroup/Fileshare (ImageFG/ImageFS) works fine, and the other one ( DocFG/DocFS ) doesn't. 几个月后,我注意到一个FileGroup / Fileshare(ImageFG / ImageFS)可以正常工作,而另一个( DocFG / DocFS )则不能。 In more detail, this is what I get 更详细地说,这就是我得到的

SELECT DocFileID, ModifiedDate FROM DocFile
-- WORKS FINE

SELECT DocFileID, ModifiedDate, Doc FROM DocFile
--Returns this error:
-- A transport-level error has occurred when receiving results from the server. (provider: Shared Memory Provider, error: 0 - No process is on the other end of the pipe.)

So, Doc is the filestream field in this table. 因此,Doc是此表中的文件流字段。 I tried to remove the field and add it again, but SSMS won't let me: 我试图删除该字段并再次添加,但SSMS不允许我这样做:

ALTER TABLE DocFile DROP COLUMN Doc
--Returns this error:
-- The FILESTREAM filegroup '**DocFG**' has no files assigned to it. FILESTREAM data cannot be populated on this filegroup until a file is added.
-- Huh?  there are already files in this Windows folder

I also tried to shrink the DocFG filegroup to try to delete it but I get this error. 我也尝试缩小DocFG文件组以尝试将其删除,但出现此错误。

DBCC SHRINKFILE ('DocFG', EMPTYFILE)
--Returns this error:
--Could not locate file 'DocFG' for database 'MyDB' in sys.database_files. The file either does not exist, or was dropped.

I have tried to detach and reattach the database, using this code, but it doesn't make a difference: 我尝试使用此代码分离并重新附加数据库,但这没有什么不同:

CREATE DATABASE [MyDB]
 ON  PRIMARY 
( NAME = N'MyDB', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL13.SQL2016\MSSQL\DATA\MyDB.mdf' , SIZE = 71040KB , MAXSIZE = UNLIMITED, FILEGROWTH = 10%), 
 FILEGROUP [ImageFG] CONTAINS FILESTREAM 
( NAME = N'ImageFS', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL13.SQL2016\MSSQL\DATA\ImageFS' , MAXSIZE = UNLIMITED), 
 FILEGROUP [DocFG] CONTAINS FILESTREAM DEFAULT
( NAME = N'DocFS', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL13.SQL2016\MSSQL\DATA\DocFS' , MAXSIZE = UNLIMITED)
 LOG ON ( NAME = N'MyDB_log', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL13.SQL2016\MSSQL\DATA\MyDB_log.ldf' , SIZE = 43264KB , MAXSIZE = UNLIMITED, FILEGROWTH = 10%)
FOR ATTACH

This ataches fine, but DocFS/DocFG still doesn't work. 这样可以达到目的,但是DocFS / DocFG仍然不起作用。 I also tried to use SSMS to manually "REMOVE" DocFG, but I get an error as well: 我还尝试使用SSMS手动“删除” DocFG,但同时也出现错误:

SSMS1 SSMS1

SSMS2 SSMS2

Any thoughts about where to proceed with this? 关于如何进行此操作有任何想法吗?

It turns out that this was an oddball situation. 事实证明,这是一个奇怪的情况。 Somehow the DocFS (files tab of Database properties) and DocFG (filegroups tab of Database properties) were not in sync (for lack of a better term). DocFS(“数据库”属性的“文件”选项卡)和DocFG(“数据库”属性的“文件组”选项卡)以某种方式不同步(因为缺少更好的术语)。 If you tried to re-create the DocFS based on DocFG, SSMS wouldn't let you. 如果您尝试基于DocFG重新创建DocFS,SSMS将不允许您这样做。 The reason being that the original corresponding Windows directory was already there, and SSMS was trying to recreate one there. 原因是原始的对应Windows目录已经存在,SSMS试图在该目录中重新创建一个目录。

Solution (for me, at least): 解决方案 (至少对我而言):

  1. Rename the windows directory ("...\\DocFS" in my case), to something temporary, such as "..\\DocFS_Temp\\") 重命名Windows目录(在我的情况下为“ ... \\ DocFS”),例如“ .. \\ DocFS_Temp \\”)
  2. go back and re-create the DocFS "file" in the files tab of Database Properties, being sure to select the corresponding FileGroup (eg DocFG). 返回并在“数据库属性”的“文件”选项卡中重新创建DocFS“文件”,请确保选择相应的FileGroup(例如DocFG)。 This will then create a new "...\\DocFS" Windows directory 然后,将创建一个新的“ ... \\ DocFS” Windows目录
  3. Delete this newly created "...\\DocFS" Windows directory 删除此新创建的“ ... \\ DocFS” Windows目录
  4. Rename the temp directory back to "...\\DocFS" 将临时目录重命名为“ ... \\ DocFS”

Other housekeeping things I did (which may or may not have helped) ...I toggled the DB offline, and then back online. 我做过的其他整理工作(可能有帮助,也可能没有帮助)...我使数据库脱机,然后又重新联机。 I also had to toggle the auto-growth from unlimited to 10% and back to shake off a "directory is full" error. 我还必须将自动增长从无限制切换到10%,然后退回以摆脱“目录已满”错误。

I hope this helps someone else. 我希望这可以帮助其他人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM