CodeCopy

March 15, 2010

To enable Snapshot Transactions in MS-SQL 2005+

Filed under: SQL — mazzoo @ 09:37

If you have problems with transactions locking your tables, you might want to use Snapshot transactions.

Snapshot transactions work like this:

When the SNAPSHOT isolation level is enabled, each time a row is updated, the SQL Server Database Engine stores a copy of the original row in tempdb, and adds a transaction sequence number to the row.

This ensures that the table is not locked while in tranaction(s).

If a snapshot transaction attempts to commit an update to a row that was changed after the transaction began, the transaction is rolled back, and an error is raised.

To enable it, use following script ( This will not hang like other examples )

declare @sql varchar(8000)

select @sql = ‘
ALTER DATABASE Libomcore SET SINGLE_USER WITH ROLLBACK
IMMEDIATE ;
ALTER DATABASE Libomcore SET READ_COMMITTED_SNAPSHOT ON;
ALTER DATABASE Libomcore SET MULTI_USER;
ALTER DATABASE Libomcore SET ALLOW_SNAPSHOT_ISOLATION ON;

Exec(@sql)

Advertisement

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Theme: Rubric. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.