<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Cursor Disconnecting - MSSQL Query</title>
	<atom:link href="https://mssqlquery.com/tag/cursor-disconnecting/feed" rel="self" type="application/rss+xml" />
	<link>https://mssqlquery.com</link>
	<description>MSSQL and TSQL Programming and TSQL Examples</description>
	<lastBuildDate>Fri, 07 Apr 2023 19:11:30 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.3.1</generator>

<image>
	<url>https://mssqlquery.com/wp-content/uploads/2023/06/cropped-mssql-query-icon-32x32.png</url>
	<title>Cursor Disconnecting - MSSQL Query</title>
	<link>https://mssqlquery.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Cursor Disconnecting Database Before Detach Operation in SQL Server</title>
		<link>https://mssqlquery.com/cursor-disconnecting-database-before-detach-operation-in-sql-server</link>
		
		<dc:creator><![CDATA[Yavuz Selim Kart]]></dc:creator>
		<pubDate>Mon, 21 Feb 2022 20:04:54 +0000</pubDate>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Cursor Disconnecting]]></category>
		<category><![CDATA[Operation in SQL Server]]></category>
		<category><![CDATA[SQL Server Cursor]]></category>
		<guid isPermaLink="false">https://mssqlquery.com/?p=658</guid>

					<description><![CDATA[<p>Hello everyone, In this article, I will talk about the use of cursor that disconnects the database before detach in SQL Server. Actually I wrote this Cursor on a question. Thinking it might be useful to someone. While detaching in SQL Server, you may encounter errors upon connection. These errors&#46;&#46;&#46;</p>
<p>The post <a href="https://mssqlquery.com/cursor-disconnecting-database-before-detach-operation-in-sql-server">Cursor Disconnecting Database Before Detach Operation in SQL Server</a> first appeared on <a href="https://mssqlquery.com">MSSQL Query</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>Hello everyone,</p>
<p>In this article, I will talk about the use of cursor that disconnects the database before detach in SQL Server.</p>
<p>Actually I wrote this Cursor on a question. Thinking it might be useful to someone.</p>
<p>While detaching in SQL Server, you may encounter errors upon connection. These errors can be solved with short written codes, or you can solve them using the Cursor I have written below.</p>
<pre class="line-numbers"><code class="language-sql">DECLARE @dbname AS VARCHAR(80);
DECLARE @server_name AS VARCHAR(20);
SELECT @server_name = @@servername;
DECLARE rs_cursor CURSOR FOR
SELECT name
FROM master.dbo.sysdatabases
WHERE name IN ( N'TestIslem' ); --In this section, you can select as many databases as you want. 
OPEN rs_cursor;
FETCH NEXT FROM rs_cursor
INTO @dbname;
IF @@FETCH_STATUS &lt;&gt; 0
    PRINT 'There is no database to terminate the connection... Please check your script!!!';
WHILE @@FETCH_STATUS = 0
BEGIN
    EXEC ('ALTER DATABASE ' + @dbname + ' SET SINGLE_USER WITH ROLLBACK IMMEDIATE');
    EXEC ('ALTER DATABASE ' + @dbname + ' SET SINGLE_USER WITH ROLLBACK AFTER 60 SECONDS');
    EXEC ('ALTER DATABASE ' + @dbname + ' SET MULTI_USER');
    FETCH NEXT FROM rs_cursor
    INTO @dbname;
    PRINT 'Print ''SERVER NAME : ' + UPPER(@server_name) + '--&gt; Successfully disconnected for all databases.''';
END;
CLOSE rs_cursor;
DEALLOCATE rs_cursor;</code></pre>
<p>When you run the code, where I used a database called TestIslem. This will terminate any open connections on the database. In this way, you will successfully perform the detach process.</p>
<p>Good luck to everyone in business and life.</p>
<div class='epvc-post-count'><span class='epvc-eye'></span>  <span class="epvc-count"> 175</span><span class='epvc-label'> Views</span></div><p>The post <a href="https://mssqlquery.com/cursor-disconnecting-database-before-detach-operation-in-sql-server">Cursor Disconnecting Database Before Detach Operation in SQL Server</a> first appeared on <a href="https://mssqlquery.com">MSSQL Query</a>.</p>]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
