recent

Titulo

Rollback - SQL Server

Rollback - SQL Server

Rollback Transactions: This command will rollback all the data modification made to a table from the start of the transaction or to a save point. This also frees any resource locked by the transactions. If you don't have any save points, the rollback will rolls back to the beginning of the transaction.
Database professional should consider this features while making modification to the table. You can either commit the transaction  if the result is what you want or you can rollback a transaction and start all over.

Syntax:
ROLLBACK { TRAN | TRANSACTION } 
     [ transaction_name | @tran_name_variable
     | savepoint_name | @savepoint_variable ] 
[ ; ]


Examples:

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

Source: msdn.microsoft.com

Interested in working with me? I can be reached at pbaniya04[at]gmail.com for any questions, consulting opportunities or you may drop a line to say HELLO. Thank your again for visiting my blog and looking forward to serving you more.

Have a Database-ious Day!

No comments

Powered by Blogger.