Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7308459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T23:33:36+00:00 2026-05-28T23:33:36+00:00

I have the following SQL script: USE MyDatabase ALTER DATABASE MyDatabase SET SINGLE_USER WITH

  • 0

I have the following SQL script:

USE MyDatabase

ALTER DATABASE MyDatabase SET SINGLE_USER WITH ROLLBACK IMMEDIATE
GO

BEGIN TRANSACTION

-- Write to tables here

COMMIT TRANSACTION

ALTER DATABASE MyDatabase SET MULTI_USER
GO

I want to make sure that my database is not accessed while the script is running, which is why I am setting the database to SINGLE_USER at the top.

Now, if anything inside the transaction fails (e.g. syntax error) I will never reset the database back to MULTI_USER which means it will be stuck in single user mode permanently (not what I want).

Is there a way to ensure we always go back to MULTI_USER, even on failure?

I guess I’m looking for the equivalent of a finally block in SQL, but from what I have read this doesn’t exist. Is there another way?

It’s worth noting that I cannot move the ALTER DATABASE commands into the transaction, as this is not permitted (since they are committed immediately).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T23:33:37+00:00Added an answer on May 28, 2026 at 11:33 pm

    You should be able to use a TRY CATCH to make sure you always go back to MULTI_USER.

    You will just need to move the command to switch back to MULTI_USER right after the TRY CATCH block, since FINALLY is not supported in SQL Server.

    I ran a quick test with the following SQL and it worked as expected in SQL Server 2005. Just make sure you ROLLBACK the transaction in the CATCH block. I used SELECT 1/0 to force the code into the CATCH block. For debugging purposes, I added the SELECT user_access_desc ... statements to show that the database was indeed switching from single user back to multi user mode.

    USE MyDatabase
    
    ALTER DATABASE MyDatabase SET SINGLE_USER WITH ROLLBACK IMMEDIATE
    GO
    
    SELECT user_access_desc from sys.databases WHERE Name = 'MyDatabase'
    
    DECLARE @errNum AS INT
    DECLARE @errMsg AS VARCHAR(MAX)
    SET @errNum = 0
    
    BEGIN TRY
    
        BEGIN TRANSACTION
        SELECT 1/0
        COMMIT TRANSACTION
    
    END TRY
    BEGIN CATCH
    
        SELECT @errNum = ERROR_NUMBER(), @errMsg = ERROR_MESSAGE()
        ROLLBACK TRANSACTION
    
    END CATCH
    
    IF @errNum <> 0
        SELECT 'An error occurred: ' + CAST(@errNum AS VARCHAR) + '- ' + @errMsg
    
    ALTER DATABASE MyDatabase SET MULTI_USER
    GO
    
    SELECT user_access_desc from sys.databases WHERE Name = 'MyDatabase'
    

    EDIT
    In my original answer, I had the ALTER ... MULTI_USER statement inside both the TRY and the CATCH block, but that was unnecessary so I moved the statement to right after the TRY CATCH block. I also added some error reporting. Some things to watch out for with this approach are:

    1. If you do any error handling or reporting, you’ll need to make sure that SQL doesn’t error. I would probably write the @errNum and @errMsg values to a table (wrapped in a TRY CATCH), switch back to MULTI_USER mode, and then perform whatever other error handling measures that are required, as the local variables will go out of scope after the GO statement.
    2. Some errors are unaffected by TRY CATCH. The documentation I linked to above does list out what those conditions are.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following script. I don't know how to create new database in sql
I am using SQL Server 2008 and I have the following SQL script: Select
I have the following SQL script: DECLARE @temp table ( ID int IDENTITY(1, 1),
I have 3 tabels in my SQL Server database with the following info: tabel:
I have a large SQL script that creates my database (multiple tables, triggers, and
I have the following SQL-statement: SELECT DISTINCT name FROM log WHERE NOT name =
I have the following sql query for transforming data but is it possible to
I have the following SQL query: select expr1, operator, expr2, count(*) as c from
I have a following SQL QUERY: SELECT articles.name, articles.price, users.zipcode FROM articles INNER JOIN
I have the following SQL Statement. I need to select the latest record for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.