Friday 16 August 2013

Transaction and Distribution

What is SQL Server transaction?
A block of or single statements execute in the database in sequence ways perform all the statement   or none of them. A transaction must satisfy four properties
-          Atomicity
Transactions either commit or roll back  
-          Consistency
It should maintain the state data from start of transaction to the end of transaction.
-          Isolation
It should isolate the modification by the two transactions.
-          Durability
If any disaster occurred in the database before the transaction it rolled back all data otherwise it will store the data in place.

Begin Transaction | Tran trans1
-          Start point of transaction.
Commit Transaction trans1
-          Store the data permanently in the database.
Rollback Transaction trans1
-          Rolled back all the data from begin transaction statements.
Save Transaction trans1
-          Store the some part of transaction data in the database.

BEGIN TRAN T1;
UPDATE table1 ...;
BEGIN TRAN M2 WITH MARK;
UPDATE table2 ...;
SELECT * from table1;
COMMIT TRAN M2;
UPDATE table3 ...;
COMMIT TRAN T1;
MSDTC – Microsoft distribution transaction coordinator
Whenever do the operation on the multiple server that time need to use the distrinbuted trancation.

BEGIN DISTRIBUTED TRANSACTION;
-- Delete candidate from local instance.
DELETE AdventureWorks.HumanResources.JobCandidate
    WHERE JobCandidateID = 13;
-- Delete candidate from remote instance.
DELETE RemoteServer.AdventureWorks.HumanResources.JobCandidate
    WHERE JobCandidateID = 13;
COMMIT TRANSACTION;

How locking is intergrated with the ISOLATION Level

In SQL Server whenever multiple uses are trying to access the piece of data at the same moment locking mechanism provides the data in synchronies ways to all the uses. Specifically Locking mechanism is using in programs at the time of deal with isolation levels.
Locking is specifically integrated with the ISOLATION LEVEL.
Types of mode of Locking
-          Shared  (S)

Shared locks apply on the read (SELECT) operation. Whenever one transaction is reading the data on same moment other transaction cannot modify the data. Once read operation is completed shared lock released from the transaction.

-          Update (U)–

At a single moment only one transaction can modify the data. Other transaction still waits to complete the current transaction.

-          Exclusive(X) –

No other transaction can’t modify the data and read operation can be take place with the help of NOLOCK hints.

At the time of both the operation read and modify, first read operation access the data before performing the modification operation in exclusive lock.

-          Schema
-          Bulk update
-          Key range
-          Intent

Get the table data in the csv format file

select top 10 'exec master..xp_cmdshell'+ ' '''+ 'bcp'
+ ' ' + TABLE_CATALOG + '.' + TABLE_SCHEMA + '.' + TABLE_NAME + ' out'+ ' c:\Temp\'+ TABLE_CATALOG + '.' + TABLE_SCHEMA + '.' + TABLE_NAME + '.csv' + ' -c -t; -T -S' + @@servername+ ''''from INFORMATION_SCHEMA.TABLES where TABLE_TYPE = 'BASE TABLE'