Shrink large database

  • Hi,

    first deattach database from the system after increase the harddisk and change the database backup location.

  • Previous reply should work with the ALTER DATABASE commands.

    However, if you are getting db in use errors a quick way of solving this is to do the following:

    Basically, this switches the db into single user mode. This disconnects any other connections. You then put your code in the bit "Put your code here". Finally, the db is set back to normal multi user mode. I have inclded the error trap so that if any errors occur the db will always go back to multuser mode as oposed to leaving the db in single user mode.

    BEGIN TRY

    ALTER DATABASE [DB NAME] SET SINGLE_USER WITH ROLLBACK IMMEDIATE

    Put your code here

    ALTER DATABASE [DB NAME] SET MULTI_USER

    END TRY

    BEGIN CATCH

    SELECT

    ERROR_NUMBER() AS ErrorNumber,

    ERROR_SEVERITY() AS ErrorSeverity,

    ERROR_STATE() as ErrorState,

    ERROR_PROCEDURE() as ErrorProcedure,

    ERROR_LINE() as ErrorLine,

    ERROR_MESSAGE() as ErrorMessage

    ALTER DATABASE [DB NAME] SET MULTI_USER

    END CATCH

Viewing 2 posts - 16 through 16 (of 16 total)

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