Can't delete secondary db file

  • I'm moving some tables from a secondary filegroup (.ndf) back into the primary filegroup (.mdf). I'm doing this by changing the filegroup that the clustered index is on for each table, and if necessary any other indexes.

    When I finished, I tried to remove the file with the command:ALTER DATABASE <db> REMOVE FILE [<logical file name>}Running this produces this error:

    Msg 5042, Level 16, State 1, Line 1

    The file 'rotc_ref_Data' cannot be removed because it is not empty.

    After searching the internet for a while, I ran this code to see what all is still in the filegroup:

    SELECT a.*, Object_Name(p.object_id), p.*

    FROM sys.allocation_units a

    INNER JOIN sys.filegroups fg

    ON fg.data_space_id = a.data_space_id

    AND fg.name = 'ROTC_REF_DATA'

    LEFT OUTER JOIN sys.partitions p

    ON p.partition_id = a.container_id

    AND a.type = 2

    This returns three records, all of type 2 (type desc="LOB_DATA"). In looking at the tables (object_name(object_id)), I see that all three tables have either a text, ntext or image column. I'm assuming that this is the LOB_DATA being referred to.

    So, what do I need to do to move this LOB_DATA back into the Primary filegroup?

    Thanks,

    Wayne
    Microsoft Certified Master: SQL Server 2008
    Author - SQL Server T-SQL Recipes


    If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
    Links:
    For better assistance in answering your questions
    Performance Problems
    Common date/time routines
    Understanding and Using APPLY Part 1 & Part 2

  • I am not aware of a way to do this other than creating a new table having the same structure as the old one. Specify the desired file groups you want to use for the text and image columns. Then you insert all rows from the old table into the new table. Drop the old table and rename the new one.

    Actually, if you do this you should be using varchar(MAX) and varbinary(MAX) instead of text and image because they are deprecated, so create the columns as these new types.

    The probability of survival is inversely proportional to the angle of arrival.

  • You can do this using an ALTER TABLE command and specifying a different filegroup for the BLOB data. ALTER TABLE can also be used to move data between filegroups where you don't have a clustered index.

  • sturner (12/23/2009)


    I am not aware of a way to do this other than creating a new table having the same structure as the old one. Specify the desired file groups you want to use for the text and image columns. Then you insert all rows from the old table into the new table. Drop the old table and rename the new one.

    Actually, if you do this you should be using varchar(MAX) and varbinary(MAX) instead of text and image because they are deprecated, so create the columns as these new types.

    This worked, though I would have preferred a method that just transferred it over without having to use an intermediate table. Thanks for your help!

    Wayne
    Microsoft Certified Master: SQL Server 2008
    Author - SQL Server T-SQL Recipes


    If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
    Links:
    For better assistance in answering your questions
    Performance Problems
    Common date/time routines
    Understanding and Using APPLY Part 1 & Part 2

  • Glenn Dorling (12/23/2009)


    You can do this using an ALTER TABLE command and specifying a different filegroup for the BLOB data. ALTER TABLE can also be used to move data between filegroups where you don't have a clustered index.

    I couldn't find in BOL where you could specify a filegroup for the BLOB data with the ALTER TABLE command. It also looks like you can only transfer the data to a different filegroup concurrent with dropping a constraint.

    Thanks for your help!

    Wayne
    Microsoft Certified Master: SQL Server 2008
    Author - SQL Server T-SQL Recipes


    If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
    Links:
    For better assistance in answering your questions
    Performance Problems
    Common date/time routines
    Understanding and Using APPLY Part 1 & Part 2

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply