Sql-Server

從數據庫中刪除文件流

  • April 21, 2019

我正在嘗試從我的數據庫中刪除文件流,所以我必須執行以下操作

我想知道如何刪除以下內容:

ALTER TABLE AMGR_Documents_Tbl ADD UId uniqueidentifier ROWGUIDCOL NOT NULL DEFAULT( NEWID() );
ALTER TABLE AMGR_Documents_Tbl ADD CONSTRAINT AMGR_Documents_Tbl_UId_Unique UNIQUE( UId );`

然後我需要添加一個沒有文件流的列,它可能是以下程式碼:

ALTER TABLE AMGR_Letters_Tbl ADD TextCol1 varbinary( max );
Update [AMGR_Letters_Tbl]
SET TextCol1 = TextCol;
ALTER TABLE [AMGR_Letters_Tbl] DROP COLUMN TextCol;
/*Now i just rename the column*/

我還需要修復 FTS 索引(我不明白),之後是以下內容:

   IF 0 != INDEXPROPERTY( OBJECT_ID( 'AMGR_Letters_Tbl' ), 'Letters_Record_Id', 'IsFulltextKey' ) BEGIN
               EXEC sp_fulltext_table AMGR_Letters_Tbl, 'drop'
           END

/****** I need to fix filestream on both tables:  ******/
/****** This is what the whole thing looks like (for 2 tables):  ******/
IF @@VERSION NOT LIKE 'Microsoft SQL Server 2005%' AND ISNULL(SERVERPROPERTY ('FilestreamEffectiveLevel'),0) > 0 BEGIN
   if not exists   ( select name from syscolumns where id IN 
               ( select Id from sysobjects where id = object_id(N'[dbo].[AMGR_Letters_Tbl]') and OBJECTPROPERTY(id, N'IsUserTable') = 1
               ) AND name = 'UId' )
   BEGIN
       -- add unique ROWGUIDCOL column
       ALTER TABLE AMGR_Letters_Tbl ADD UId uniqueidentifier ROWGUIDCOL NOT NULL DEFAULT( NEWID() );
       ALTER TABLE AMGR_Letters_Tbl ADD CONSTRAINT AMGR_Letters_Tbl_UId_Unique UNIQUE( UId );
       EXEC( 'ALTER TABLE AMGR_Letters_Tbl ADD TextCol1 varbinary( max ) FILESTREAM NULL' );
       -- will need to re-create FTS index after
       IF 0 != INDEXPROPERTY( OBJECT_ID( 'AMGR_Letters_Tbl' ), 'Letters_Record_Id', 'IsFulltextKey' ) BEGIN
           EXEC sp_fulltext_table AMGR_Letters_Tbl, 'drop'
       END
       EXEC( 'UPDATE AMGR_Letters_Tbl SET TextCol1 = TextCol;' );
       ALTER TABLE AMGR_Letters_Tbl DROP COLUMN TextCol;
       EXEC sp_rename 'AMGR_Letters_Tbl.TextCol1', 'TextCol', 'COLUMN';
       EXEC ('DBCC CLEANTABLE (0,''AMGR_Letters_Tbl'',1000) WITH NO_INFOMSGS;');
   END
END 
GO
------------------------------------------------------------

IF @@VERSION NOT LIKE 'Microsoft SQL Server 2005%' AND ISNULL(SERVERPROPERTY ('FilestreamEffectiveLevel'),0) > 0 BEGIN
   if not exists   ( select name from syscolumns where id IN 
               ( select Id from sysobjects where id = object_id(N'[dbo].[AMGR_Documents_Tbl]') and OBJECTPROPERTY(id, N'IsUserTable') = 1
               ) AND name = 'UId' )
   BEGIN
       -- add unique ROWGUIDCOL column
       ALTER TABLE AMGR_Documents_Tbl ADD UId uniqueidentifier ROWGUIDCOL NOT NULL DEFAULT( NEWID() );
       ALTER TABLE AMGR_Documents_Tbl ADD CONSTRAINT AMGR_Documents_Tbl_UId_Unique UNIQUE( UId );
       EXEC( 'ALTER TABLE AMGR_Documents_Tbl ADD Data1 varbinary( max ) FILESTREAM NULL' );
       -- will need to re-create FTS index after
       IF 0 != INDEXPROPERTY( OBJECT_ID( 'AMGR_Documents_Tbl' ), 'AMGR_Documents_Record_Id', 'IsFulltextKey' ) BEGIN
           EXEC sp_fulltext_table AMGR_Documents_Tbl, 'drop'
       END
       EXEC( 'UPDATE AMGR_Documents_Tbl SET Data1 = Data;' );
       ALTER TABLE AMGR_Documents_Tbl DROP COLUMN Data;
       EXEC sp_rename 'AMGR_Documents_Tbl.Data1', 'Data', 'COLUMN';
       EXEC ('DBCC CLEANTABLE (0,''AMGR_Documents_Tbl'',1000) WITH NO_INFOMSGS;');
   END
END 
GO

要從數據庫中完全刪除 FILESTREAM 功能,您需要執行以下步驟。

  • 從所有表中刪除所有 FILESTREAM 列
  • 取消表與 FILESTREAM 文件組的關聯
  • 刪除所有 FILESTREAM 數據容器(文件組文件 - 您可能擁有多個文件)
  • 刪除所有 FILESTREAM 文件組(可能不止一個)

演練點擊此處並按照他的步驟操作

另一個注意事項:如果您的 FileStream 文件中有數據,您將無法刪除它。sp_filestream_force_garbage_collection不幸的是,這只適用於> = SQL Server 2012

對於該修復,您可以在此處查看全部內容

如果這不起作用。嘗試為它做一個備份。這就是我清除 FileStream 文件的方式。

當它被清除時,您可以刪除它 FileStream 文件和 FILESTREAM 文件組。

如果你的數據庫索引很重,你應該檢查你的函式和視圖,有些可能會失去。(有一個備份)

引用自:https://dba.stackexchange.com/questions/111276