Forcing an exclusive table lock. : TRANSACTION « Transaction « SQL Server / T-SQL Tutorial






CREATE TABLE authors(
   au_id          varchar(11),
   au_lname       varchar(40)       NOT NULL,
   au_fname       varchar(20)       NOT NULL,
   phone          char(12)          NOT NULL DEFAULT ('UNKNOWN'),
   address        varchar(40)           NULL,
   city           varchar(20)           NULL,
   state          char(2)               NULL,
   zip            char(5)               NULL,
   contract       bit               NOT NULL
)
GO
insert authors values('1',  'Joe',   'Abra',   '111 111-1111', '6 St.', 'Berkeley',  'CA', '11111', 1)
insert authors values('2',  'Jack',  'Majo',   '222 222-2222', '3 St.', 'Oakland' ,  'CA', '22222', 1)
insert authors values('3',  'Pink',  'Cherry', '333 333-3333', '5 Ln.', 'Vancouver', 'BC', '33333', 1)
insert authors values('4',  'Blue',  'Albert', '444 444-4444', '7 Av.', 'Vancouver', 'BC', '44444', 1)
insert authors values('5',  'Red',   'Anne',   '555 555-5555', '6 Av.', 'Regina',    'SK', '55555', 1)
insert authors values('6',  'Black', 'Michel', '666 666-6666', '3 Pl.', 'Regina',    'SK', '66666', 1)
insert authors values('7',  'White', 'Sylvia', '777 777-7777', '1 Pl.', 'Rockville', 'MD', '77777', 1)
insert authors values('8',  'Yellow','Heather','888 888-8888', '3 Pu',  'Vacaville', 'CA', '88888', 0)
insert authors values('9',  'Gold',  'Dep',    '999 999-9999', '5 Av.', 'Oakland',   'CA', '99999', 0)
insert authors values('10', 'Siler', 'Dean',   '000 000-0000', '4 Av.', 'Oakland',   'CA', '00000', 1)
GO




BEGIN TRAN  
SELECT    * 
FROM      authors (tablockx)    
WHERE     au_lname = 'Green'    
WAITFOR DELAY '00:02:00'    
ROLLBACK TRAN   
GO  

drop table authors;
GO








23.1.TRANSACTION
23.1.1.A transaction is bound by the ACID test. ACID stands for Atomicity, Consistency, Isolation (or Independence), and Durability:
23.1.2.Explicit Transaction Commands
23.1.3.Using Transactions
23.1.4.BEGIN TRANSACTION
23.1.5.Using Explicit Transactions
23.1.6.Forcing an exclusive table lock.
23.1.7.Transaction spread across batches:
23.1.8.Exception before transaction committing
23.1.9.statements coded as a transaction
23.1.10.A script with nested transactions
23.1.11.One batch that contains two transactions
23.1.12.What happens with stored proc transactions and exceptions?
23.1.13.Declare variable in a transaction
23.1.14.Inserting a Row into MyTable and Rolling Back the Transaction