I need to programmatically enable READ COMMITTED SNAPSHOT in SQL Server. How can I do that?
I recommend switching to single-user mode first. That ensures you're the only connection. Otherwise, the query might be suspended.
From: http://msdn.microsoft.com/en-us/library/ms175095.aspx
When setting the
READ_COMMITTED_SNAPSHOT option, only
the connection executing the ALTER
DATABASE command is allowed in the
database. There must be no other open
connection in the database until ALTER
DATABASE is complete.
So, use this SQL:
ALTER DATABASE <dbname> SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
ALTER DATABASE <dbname> SET READ_COMMITTED_SNAPSHOT ON;
ALTER DATABASE <dbname> SET MULTI_USER;
ALTER DATABASE [dbname] SET READ_COMMITTED_SNAPSHOT ON WITH ROLLBACK AFTER 20 SECONDS
Related
I deleted SSMS a while back and all the SQL related stuff. However, there is a db still lying around in my PC and it isn't deleting. It throws the error that DB is open in SSMS. How do I force delete it?
To close connections:
USE [mydb];
ALTER DATABASE [mydb] SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
Then drop:
USE master;
DROP DATABASE [mydb];
We have some scripts that are executed during the installation of our application.
One of them is used for setting Database Collation, the other one is used for setting compatibility level. We use the following construction for these scripts:
ALTER DATABASE [DB_NAME] SET SINGLE_USER WITH ROLLBACK IMMEDIATE
GO
ALTER DATABASE [DB_NAME] SET COMPATIBILITY_LEVEL = DB_COMPATIBILITY_LEVEL
-- ALTER DATABASE [DB_NAME] COLLATE 'Collation Name'
GO
ALTER DATABASE [DB_NAME] SET MULTI_USER
GO
The question is:
Is it possible for some background process to get an access after setting single user mode? In this case our script couldn't be executed. I saw this issue and it seems similar, so I decided to ask a question.
https://social.msdn.microsoft.com/Forums/sqlserver/en-US/950c9b85-67f9-4272-8dff-14aa3590bc8a/single-user-mode-session-lost-after-backgound-processes-jump-in?forum=sqldatabaseengine
This issue was related to restoring database.
alter database [test-db] set single_user with rollback immediate; --This sql is run using test-db
use master;restore database [test-db] from database_snapshot = 'snapshot_test-db';
alter database [test-db] set multi_user;
But I don't fully understand the difference between restoring and altering the database in the single user mode.
In the first case will be enough to set the database offline.
Any help will be appreciated.
Thanks in advance.
Using the code below I'm unable to turn off snapshot isolation. I'm using a SQL Server 2012 box. I can create brand new empty db, turn snapshot isolation on, but I can't turn it back off.
The "allow_snapshot_isolation OFF" line just spins it's wheels.
ALTER DATABASE SNAP SET SINGLE_USER WITH ROLLBACK IMMEDIATE
ALTER DATABASE SNAP SET allow_snapshot_isolation OFF
ALTER DATABASE SNAP SET read_committed_snapshot off
ALTER DATABASE SNAP SET MULTI_USER
Are you sure that no other transactions were ran on the database? Remeber about implicit transactions which are used eg. by JDBC drivers (when you setAutocommit to false).
Snapshot isolation cannot be turn off if any of the previous transactions is pending. This is, because it has to be sure that any other transaction will not try to use previous row versions. However, it is possible to make queries spanning through more than one database so setting snapshot isolation is not only taking care of transaction on one database.
You can check if this is the case by using sp_who2 and SELECT * FROM sys.sysprocessesand searching for your altering process. sp_who2 will be showing that the process is suspended and by using sys.sysprocesses you will probably find out that its lastwaiting type is 'DISABLE_VERSIONING'.
So your solution is to rollback all transactions (or close conections in case of implicit transactions).
Be awared, that if you are using a connection pooling from some program, you may have, for example, 40 implicit transactions opened in two or three dbs. If they were opened after the snapshot mode had been turned on then turning it off will be impossible until they end.
I followed below for the same issue I was getting
USE master;
GO
ALTER DATABASE MYDB SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
GO
ALTER DATABASE MYDB MODIFY NAME = MYDB_1;
GO
ALTER DATABASE MYDB_1 SET READ_COMMITTED_SNAPSHOT OFF;
GO
ALTER DATABASE MYDB_1 MODIFY NAME = MYDB;
GO
ALTER DATABASE MYDB SET MULTI_USER;
I ran the MSSQL query below to turn off ALLOW_SNAPSHOT_ISOLATION for master database:
ALTER DATABASE master SET ALLOW_SNAPSHOT_ISOLATION OFF;
GO;
Then, I got this error below:
SNAPSHOT ISOLATION is always enabled in this database.
And, I ran the MSSQL query below to turn on READ_COMMITTED_SNAPSHOT for master database:
ALTER DATABASE master SET READ_COMMITTED_SNAPSHOT ON;
GO;
Then, I got this error below:
Option 'READ_COMMITTED_SNAPSHOT' cannot be set in database 'master'.
Because the documentation says below:
The ALLOW_SNAPSHOT_ISOLATION option is automatically set ON in the
master and msdb databases, and cannot be disabled.
Users cannot set the READ_COMMITTED_SNAPSHOT option ON in master,
tempdb, or msdb.
Using
Microsoft SQL Server 2008 (SP1)
Trying to create transaction like so:
OleDbConnection con;
OleDbTransaction transaction = null;
...
transaction = con.BeginTransaction(IsolationLevel.Snapshot);
And get error:
System.Data.OleDb.OleDbException (0x8004D008): Neither the isolation level nor a strengthening of it is supported.
at System.Data.OleDb.OleDbTransaction.ProcessResults(OleDbHResult hr)
at System.Data.OleDb.OleDbTransaction.BeginInternal(ITransactionLocal
transaction)
at System.Data.OleDb.OleDbConnectionInternal.BeginTransaction(IsolationLevel
isolationLevel)
at System.Data.OleDb.OleDbConnection.BeginTransaction(IsolationLevel
isolationLevel)
I made sure to run the following in the SQL Server Management Studio:
ALTER DATABASE mydb SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
ALTER DATABASE mydb SET ALLOW_SNAPSHOT_ISOLATION ON;
ALTER DATABASE mydb SET READ_COMMITTED_SNAPSHOT ON;
ALTER DATABASE mydb SET MULTI_USER;
Anything I'm missing?
Replace OleDb with SqlClient (as Oded suggested in a comment).
Or alternatively, execute this SQL command before beginning the transaction:
SET TRANSACTION ISOLATION LEVEL SNAPSHOT;
I'm trying to run the following statement on a SQL server database:
ALTER DATABASE myDB
SET READ_COMMITTED_SNAPSHOT ON
Query Analyzer just keeps spinning and nothing returns. I've read about other people experiencing this problem and that it should normally return fairly quickly, and the only way they've found around it is to restart the SQL Server service. Unfortunately, I don't have that option. Is there any way to get this statement to run without restarting the service?
Looks like my search parameters weren't too good, came up with an answer now. The code below seems to do the trick:
ALTER DATABASE myDB SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
ALTER DATABASE myDB SET READ_COMMITTED_SNAPSHOT ON;
ALTER DATABASE myDB SET MULTI_USER;