{"id":316,"date":"2014-06-20T08:37:08","date_gmt":"2014-06-20T08:37:08","guid":{"rendered":"http:\/\/www.nicovs.be\/?p=316"},"modified":"2014-06-20T08:38:42","modified_gmt":"2014-06-20T08:38:42","slug":"mssql-drop-connections","status":"publish","type":"post","link":"https:\/\/www.nicovs.be\/?p=316","title":{"rendered":"MsSQL Database: Drop connections"},"content":{"rendered":"<p>If you need to restore a Microsoft SQL Database, you might run into the problem that there are still users connection to the database.<\/p>\n<p>It&#8217;s not easy to drop those connections&#8230; lots of info that I find on google use<\/p>\n<pre class=\"lang:tsql decode:true\">ALTERDATABASE DB_NAME SET SINGLE_USER WITHROLLBACK IMMEDIATE\r\n--do stuff\r\nALTERDATABASEDB_NAME SET MULTI_USER<\/pre>\n<p>And this doesn&#8217;t always seem to work on SQL2008 or SQL2012.<\/p>\n<p>But&#8230; here&#8217;s a handy script that I found on <a title=\"Stackoverflow\" href=\"http:\/\/stackoverflow.com\/questions\/11620\/how-do-you-kill-all-current-connections-to-a-sql-server-2005-database\" target=\"_blank\">Stackoverflow<\/a>:<\/p>\n<p>Replace the DB_Name variable with the name of your DB for which you want to drop the connection:<\/p>\n<pre class=\"lang:tsql decode:true\">USE master\r\nGO\r\n\r\nSET NOCOUNT ON\r\nDECLARE @DBName varchar(50)\r\nDECLARE @spidstr varchar(8000)\r\nDECLARE @ConnKilled smallint\r\nSET @ConnKilled=0\r\nSET @spidstr = ''\r\n\r\nSet @DBName = 'DB_NAME'\r\nIF db_id(@DBName) &lt; 4\r\nBEGIN\r\nPRINT 'Connections to system databases cannot be killed'\r\nRETURN\r\nEND\r\nSELECT @spidstr=coalesce(@spidstr,',' )+'kill '+convert(varchar, spid)+ '; '\r\nFROM master..sysprocesses WHERE dbid=db_id(@DBName)\r\n\r\nIF LEN(@spidstr) &gt; 0\r\nBEGIN\r\nEXEC(@spidstr)\r\nSELECT @ConnKilled = COUNT(1)\r\nFROM master..sysprocesses WHERE dbid=db_id(@DBName)\r\nEND<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you need to restore a Microsoft SQL Database, you might run into the problem that there are still users connection to the database. It&#8217;s not easy to drop those connections&#8230; lots of info that I find on google use ALTERDATABASE DB_NAME SET SINGLE_USER WITHROLLBACK IMMEDIATE &#8211;do stuff ALTERDATABASEDB_NAME SET MULTI_USER And this doesn&#8217;t always [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[33,32],"tags":[],"class_list":["post-316","post","type-post","status-publish","format-standard","hentry","category-mssql","category-windows"],"_links":{"self":[{"href":"https:\/\/www.nicovs.be\/index.php?rest_route=\/wp\/v2\/posts\/316","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.nicovs.be\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.nicovs.be\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.nicovs.be\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.nicovs.be\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=316"}],"version-history":[{"count":4,"href":"https:\/\/www.nicovs.be\/index.php?rest_route=\/wp\/v2\/posts\/316\/revisions"}],"predecessor-version":[{"id":320,"href":"https:\/\/www.nicovs.be\/index.php?rest_route=\/wp\/v2\/posts\/316\/revisions\/320"}],"wp:attachment":[{"href":"https:\/\/www.nicovs.be\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=316"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.nicovs.be\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=316"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.nicovs.be\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=316"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}