Thursday, September 06, 2012

Fwd: ROLLBACK In MSSQL



Salam 1 Malaysia
Sebelum ni memang aku ada masalah jika aku tersalah commit transaction
in MSSQL. Memang diorang banyak bagitau yang boleh guna ROLLBACK kalau
camtu.. But Do you all know that ROLLBACK can be use only when you
declare.. Kalau tak declare.. Assalam la kat.. Selamat la.. Below is
one of the example that ROLLBACK Transaction for MSSQL.. Tapi ingat
declare the transaction dulu..

USE tempdb;
GO
CREATE TABLE ValueTable ([value] int;)
GO
DECLARE @TransactionName varchar(20) = 'Transaction1';
--The following statements start a named transaction,
--insert two rows, and then roll back
--the transaction named in the variable @TransactionName.
--Another statement outside of the named transaction inserts two rows.
--The query returns the results of the previous statements.
BEGIN TRAN @TransactionName
       INSERT INTO ValueTable VALUES(1), (2);
ROLLBACK TRAN @TransactionName;
INSERT INTO ValueTable VALUES(3),(4);
SELECT [value] FROM ValueTable;
DROP TABLE ValueTable;
--Results
--value
-------------
--3
--4

Tapi kalau ada sesiapa yang boleh ROLLBACK Transaction tu tanpa buat
begitu.. Amatlah dialu-alukan mengajar saya yang agak tidak pandai
ini.. Ini semua dari research2 informal yang saya buat k.

Selamat Mencuba... (0.<)
Share: