Windows IT Pro is the authoritative and independent resource for windows nt, windows 2000, windows 2003, windows xp. Features a collection of resources and magazines for windows IT professionals.
  
  
  Advanced Search 


April 21, 2008

Jump Start: Tracking Transactions

RSS
Subscribe to Windows IT Pro | See More SQL Server and Database Articles Here | Reprints | Or get the Monthly Online Pass—only $5.95 a month!

My "Using Transactions" article (InstantDoc ID 98827, www.sqlmag.com/articles/index.cfm?articleid=98827) prompted some questions from readers about how transactions work, when and where the data is written, and whether the data is put into the database before the transaction is committed.

 

SQL Server and SQL Server Express databases are stored in two types of files. Database data is stored in data files, which have an .mdf extension. Transactions are stored in transaction log files, which have an .ldf extension. The data is written to both the transaction log file and the data file.

 

When you use a BEGIN TRANSACTION statement to start a transaction, you essentially mark a set of related data in the transaction log. SQL Server can use that transaction log marker to pull the data out of the data file in the event of a ROLLBACK TRANSACTION operation.

 

The sample code below can help illustrate how transactions work. The code contains a multistatement transaction that consists of two INSERT statements.

 

USE Northwind

GO

 

BEGIN TRANSACTION MyTempRegion;

 

INSERT INTO Region (RegionID, RegionDescription)

 VALUES (5, 'Middle');

 

INSERT INTO Territories (TerritoryID, TerritoryDescription, RegionID)

 VALUES (55555, 'Chicago', 5);

 

-- The rows are added

SELECT TerritoryDescription, RegionDescription

FROM Territories INNER JOIN Region

ON Territories.RegionID = Region.RegionID

WHERE Territories.RegionID = 5

 

ROLLBACK TRANSACTION MyTempRegion;

 

-- The rows are gone

SELECT TerritoryDescription, RegionDescription

FROM Territories INNER JOIN Region

ON Territories.RegionID = Region.RegionID

WHERE Territories.RegionID = 5

 

The code first starts a transaction, which inserts data into two tables. The transaction then executes a SELECT statement. If you run the code, the results of that SELECT statement will show that data has been written to the database--no COMMIT statement is needed to write the data to the database. Because the INSERT and SELECT statements are within a transaction, the data is also written to the transaction log file and marked as a transaction.

 

Next, the ROLLBACK TRANSACTION statement is run, which causes the data that's marked in the transaction log to be pulled out of the database. Finally, another SELECT statement is executed. If you run the code, the second SELECT statement will show that the data that had originally been added to the tables is no longer there.

End of Article



Reader Comments

You must log on before posting a comment.

If you don't have a username & password, please register now.




Top Viewed ArticlesView all articles
A Simple File Transfer Solution

My small business clients thought FTP was the answer to their file transfer problems, but I surprised them with an even better solution for their data delivery needs. ...

The Memory-Optimization Hoax

Don't believe the hype. At best, RAM optimizers have no effect. At worst, they seriously degrade performance. ...

WinInfo Short Takes: 080808 Special Edition

An often irreverent look at some of the week's other news, including a surprising side-trip to Northern Ireland, the 2008 Summer Olympics, Microsoft vs. VMWare, Samsung's lousy SSDs, IBM and Lenovo sitting in a Microsoft-free tree, and much, much more ...


SQL Server and Database Whitepapers SQL Server® 2005 – 64-Bit Migration Best Practices

It’s What You Make IT

Database Professionals: Experience Profile and Need Gaps in Development and Database Tools

Related Events Check out our list of Free Email Newsletters!

SQL Server and Database eBooks Safeguarding Your Windows Servers

SQL Server Administration for Oracle DBAs

Taking Control: Monitoring the Windows Platform Proactively

Related SQL Server and Database Resources Become a VIP member of the Windows IT Pro community!
Get it all with the VIP CD and VIP access. A $500+ value for only $279!

Subscribe to Windows IT Pro!
Solve your toughest technical problems with our experts and access 10,000 + articles online. 30% off

Monthly Online Pass - Only $5.95!
Get instant access to 10,000+ articles from Windows IT Pro Magazine!

TechNet Virtual Labs
Evaluate and test Microsoft's newest products.

Job Openings in IT


ADS BY GOOGLE SPONSORED LINKS FEATURED LINKS

WinConnections Conference Fall 2008
Don’t miss the premier event for Microsoft IT Professionals in Las Vegas, November 10-13. Register and book your room by August 25 and receive a FREE room night (based on a three night minimum stay).

Deploying SharePoint! In-Person Event Series – 8 Cities
Discover best practices and tips for deploying the perfect SharePoint infrastructure. Early Bird Price of $99 through Aug 29th.

Find a new job now on the all new IT Job Hound!
Search jobs, post your resume, and set up job e-mail alerts!

Master SharePoint with 3 eLearning Seminars
Learn how to build a better SharePoint infrastructure and enable powerful collaboration with MVPs Dan Holme and Michael Noel. Register today!

Top Tools for Virtualization Disaster Recovery & Replication
View this web seminar on August 14th to learn about two tools that will result in faster backup and restore with P2V disaster recovery.

SharePointConnections Conference Fall 2008
Don’t miss the premier event for Microsoft IT Professionals in Las Vegas, November 10-13. Register and book your room by August 25 and receive a FREE room night (based on a three night minimum stay).

VMworld 2008 - Sign Up Today!
Join your peers on September 15-18 at The Venetian Hotel in Las Vegas as VMware hosts VMworld 2008, the leading Virtualization event.



Entrust Unified Communications Certs
Secure Exchange 2007 and save 20%. Now through Sept. 2008.

When managing just VMware isn’t enough
Plan/Manage/Secure – NetIQ VMware management. Download whitepaper.

Microsoft® Tech•Ed EMEA 2008 IT Professionals
Advance your thinking with new ideas and practical real-world solutions at Microsoft’s FIVE day technical infrastructure conference 3-7 Nov., 2008. Register before 26 September 2008 to save €300.

Order Your Fundamentals CD Today!
Gain an introduction to Exchange, learn server security requirements, and understand how unified communications can play a role in your messaging strategies with this free Exchange CD.

Are You Really Compliant with Software Regulations?
View this web seminar that will help you with compliance best practices and check out a management solution to assure that you won’t be in jeopardy of an audit.
Windows IT Pro Home Register FAQ for Windows WinInfo News
Europe Edition About Us Contact Us/Customer Service Media Kit Affiliates / Licensing  
SQL Server Magazine Office & SharePoint Pro Windows Dev Pro IT Job Hound ITTV
IT Library Technical Resources Directory Connected Home Windows Excavator Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 Copyright © 2008 Penton Media, Inc., All rights reserved. Terms and Use | Privacy Statement | Reprints and Licensing