A way to auto disconnect all connections to a database in SQLsrv2005? And self intro

  • Hi.

    I am new to the forums.

    Thank you for having me.

    I have provided some bio info in my profile for those interested in personal stuff.

    My question:

    Is it possible to schedule a nightly process to disconnect all live connections to a database? I am asking this because sometime we need to detach databases for maintenance requirements, and re-attach them again.

    I am new to SQL server to be honest, and I personally prefer to use TOAD for SQL Server (a front-end from Quest software).

    Thanks in advance and look forward to learning a lot here.

    Cheers from Australia! 🙂

    http://www.tecnq.com.au

  • My question:

    Is it possible to schedule a nightly process to disconnect all live connections to a database? I am asking this because sometime we need to detach databases for maintenance requirements, and re-attach them again.

    I am new to SQL server to be honest, and I personally prefer to use TOAD for SQL Server (a front-end from Quest software).

    Thanks in advance and look forward to learning a lot here.

    Cheers from Australia! 🙂

    -------

    Here's one way. There are several options and this may not be the best, link is included for your reference.

    This will set the database offline, and rollback any transactions in progress immediately, assuming you can.

    ALTER DATABASE [DBNAMEgoesHERE] SET OFFLINE WITH ROLLBACK IMMEDIATE

    http://msdn.microsoft.com/en-us/library/bb522682.aspx

    Hope that helps? Aloha! 🙂

    --------------------------------------------------
    ...0.05 points per day since registration... slowly crawl up to 1 pt per day hopefully 😀

  • Thank you for your reply.

    However, this does not answer my question exactly.

    What I am after is a way to say to the database, ".. at 6pm tonight I want you to disconnect all active connections (roll back is good) and be idle (open?) after all connections have severed."

    The MSDN documentation doesn't seem to offer such a feature.

    Thanks

    http://www.tecnq.com.au

  • TecNQ (5/8/2013)


    Thank you for your reply.

    However, this does not answer my question exactly.

    What I am after is a way to say to the database, ".. at 6pm tonight I want you to disconnect all active connections (roll back is good) and be idle (open?) after all connections have severed."

    The MSDN documentation doesn't seem to offer such a feature.

    Thanks

    I missed the portion asking about time. Have you worked with SQL Server Agent before? You can schedule jobs within the SQL Server agent to accomplish your goal maybe?

    This requires creating SQL Agent Job, which allows you to schedule a specific time to schedule tasks.

    http://technet.microsoft.com/en-us/library/ms189237.aspx%5B/url%5D

    --------------------------------------------------
    ...0.05 points per day since registration... slowly crawl up to 1 pt per day hopefully 😀

  • I'd set up a SQL Agent job scheduled for whatever time you need this to happen.

    One step that executes:

    USE <database_name>;

    BEGIN

    ALTER DATABASE <database_name> SET SINGLE_USER WITH ROLLBACK IMMEDIATE;

    ALTER DATABASE <database_name> SET MULTI_USER;

    END

    Quick and dirty, but it works.

    I've also seen an approach that kills sessions in a loop based on the output of sp_who2, but its a complete pita if you have an app that wants to maintain a connection.

  • Thanks guys. I didn't know of the SQL Agent. I'll try it out and let you know how I went. Cheers

    http://www.tecnq.com.au

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

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