By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,856 Members | 2,149 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,856 IT Pros & Developers. It's quick & easy.

T-SQL CLOSE Connection to DB

P: n/a
How do i close a current connection to a database using t-sql?
I fail some time to drop the database getting messages that it's
currently in use.
Using the wizard to delete the database, i could check the option to
close all connections to the db, but how do i do it using t-sql?

best regards

May 28 '06 #1
Share this Question
Share on Google+
10 Replies


P: n/a
coosa (co*****@gmail.com) writes:
How do i close a current connection to a database using t-sql?


USE <someotherdb>

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
May 28 '06 #2

P: n/a
I did actually,

USE [Master];
GO

IF EXISTS (SELECT name FROM sys.databases WHERE name = N'MyDB')
BEGIN
PRINT N'Database [MyDB] already exists';
DROP DATABASE MyDB;
END
GO

May 28 '06 #3

P: n/a
Similary, is there a similar command like 'disconnect' such as in DB2?

May 28 '06 #4

P: n/a
coosa (co*****@gmail.com) writes:
Similary, is there a similar command like 'disconnect' such as in DB2?


I don't know what DISCONNECT in DB2, but the only way to disconnect from
the server with a T-SQL command is SHUTDOWN WITH NOWAIT. Or a RAISERROR
with a severity level >= 20. I would not recommend any of them.

The proper way to disconnect is to do it from the client.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
May 28 '06 #5

P: n/a
On 27 May 2006 19:48:27 -0700, coosa wrote:
How do i close a current connection to a database using t-sql?
I fail some time to drop the database getting messages that it's
currently in use.


Hi coosa,

If you want to disconnect YOUR OWN connection to a database, check out
Erland's reply.

If you need to do maintenance but can't because OTHER people still have
open connections to a database, then you might want to use one of the
following

ALTER DATABASE <dbname> SET SINGLE_USER

or

ALTER DATABASE <dbname> SET SINGLE_USER WITH ROLLBACK_IMMEDIATE

The first version will allow curent connection to finish their business
and commit their work. The latter option immediately disconnects all
open conenctions and rolls back any changes from unfinished
transactions.

--
Hugo Kornelis, SQL Server MVP
May 28 '06 #6

P: n/a
That might be it; some thing remotly is still in use. Then, is there a
way to determine which connections are being used and wait for them but
prevent any new connections?

May 29 '06 #7

P: n/a
On 28 May 2006 18:46:15 -0700, coosa wrote:
That might be it; some thing remotly is still in use. Then, is there a
way to determine which connections are being used
Hi coosa,

EXEC sp_who2;
and wait for them but
prevent any new connections?


ALTER DATABASE <dbname> SET SINGLE_USER;

(By _not_ adding the WITH ROLLBACK_IMMEDIATE option, you tell SQL Server
to disallow new connections but wait until existing connections are
broken before setting the DB to single user)

Note that many front-end programs keep their connection open, mostly
being idle while the person on the screen enters data, answers a phone
call or visits the water cooler. If you waiting for those connections to
close, you won't have your DB in single-user state before the office
closes.

--
Hugo Kornelis, SQL Server MVP
May 29 '06 #8

P: n/a
It's interesting what's happening ...
i run:
USE Master;
GO
EXEC sp_who2;
GO

The 'MyDb' is still under the status RUNNABLE for the command 'SELECT
INTO' under the ProgamName 'Microsoft SQL Server Management Studio -
Query'.
I run the command again after a minute and it disappears.
It seems when i swith the use to a different DB, the change has no
IMMEDIATE effect.
Again, using the "Management Studio", by right clicking the Database
Name and choosing to "Delete", two check boxes can be selected and the
latter is "Close existing Connections" and it never failed to delete.
I have used the suggestion of usning both "ALTER DATABASE <dbname> SET
SINGLE_USER WITH ROLLBACK_IMMEDIATE" and "ALTER DATABASE <dbname> SET
SINGLE_USER" but it's the same. Erland suggestion recommnds stoping the
entire server which i can't afford since there are other databases
running.

May 29 '06 #9

P: n/a
On 29 May 2006 08:22:10 -0700, coosa wrote:
It's interesting what's happening ...
i run:
USE Master;
GO
EXEC sp_who2;
GO

The 'MyDb' is still under the status RUNNABLE for the command 'SELECT
INTO' under the ProgamName 'Microsoft SQL Server Management Studio -
Query'.
I run the command again after a minute and it disappears.
It seems when i swith the use to a different DB, the change has no
IMMEDIATE effect.
Hi coosa,

Very strange. I have never experienced or heard this before. And I was
unable to reproduce - when I ran the code above, sp_who2 reported the
connection to be runnable in the master DB.
Again, using the "Management Studio", by right clicking the Database
Name and choosing to "Delete", two check boxes can be selected and the
latter is "Close existing Connections" and it never failed to delete.
Under the hood, Management Studio uses the ALTER DATABASE command I
suggested, with the ROLLBACK_IMMEDIATE option. This is easy to verify:
make a DB, open some windows in MS to connect to this test DB, then
right-click the DB, click "Delete", check "Close existing connections",
then instead of clicking "OK", click "Script / Script to Clipboard".
Finally, paste the contents of the clipboard in a query window or in a
text file. Here's what was generated on my computer:

EXEC msdb.dbo.sp_delete_database_backuphistory @database_name = N'Temp'
GO
USE [master]
GO
ALTER DATABASE [Temp] SET SINGLE_USER WITH ROLLBACK IMMEDIATE
GO
USE [master]
GO
/****** Object: Database [Temp] Script Date: 05/30/2006 00:53:16
******/
DROP DATABASE [Temp]
GO
I have used the suggestion of usning both "ALTER DATABASE <dbname> SET
SINGLE_USER WITH ROLLBACK_IMMEDIATE" and "ALTER DATABASE <dbname> SET
SINGLE_USER" but it's the same.
What does "the same" mean? Do you get any error messages? If so, what
messages?

What happpens if you open a query window in SSMS, then type (or copy)
and execute the query below (replacing MyDB [twice!] with the actual
name of the DB you want to drop). If you get any errors, please copy and
paste the exact messages into a reply to this message (unless you're
running a localized Cyrillic or similar installation - in that case, a
translation is actually preferred <g>)

USE master
go
ALTER DATABASE MyDB SET SINGLE_USER WITH ROLLBACK_IMMEDIATE
go
DROP DATABASE MyDB
go

Of course, you should replace MyDB with the real name of your database
(two times!)
Erland suggestion recommnds stoping the
entire server which i can't afford since there are other databases
running.


Erland though you were asking how to force connection to the _SERVER_ to
be broken. For dropping a database, it suffices to break the connection
to the _database_. I know Erland well enough to be 100% sure that he'd
never recommend shutting down a server to drop connections to a DB.

--
Hugo Kornelis, SQL Server MVP
May 29 '06 #10

P: n/a
my appologies then to Erland for this misunderstanding.

May 30 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.