6

I have a SQL Server 2012 Sequence object:

/****** Create Sequence Object ******/
CREATE SEQUENCE TestSeq
START WITH 1
INCREMENT BY 1;

I have a SP that runs some queries inside a transaction:

BEGIN TRAN

SELECT NEXT VALUE FOR dbo.TestSeq

<here all the query update code......>

ROLLBACK TRAN

If the transaction fails all the updates are rolledback without problem but the Sequence is not rolled back I guess because Its out of the scope of the transaction.

Any clue on way to handle that?

4
  • 4
    you cannot rollback sequence , it doesn't support transactions, what you can do is store current sequence in a variable and you can alter sequence in exception block if any error occurs ex: Alter SEQUENCE TestSeq RESTART WITH @var INCREMENT BY 1;
    – rs.
    Aug 25, 2012 at 0:49
  • 2
    I think will be better to leave it like this because what happen if I alter the sequence and in that moment other connection generate next value?
    – VAAA
    Aug 25, 2012 at 1:22
  • yes you are right, it is better to leave it as is.
    – rs.
    Aug 25, 2012 at 1:29
  • 4
    Sequences are faster (and generate less locking conflicts) because they cannot be rolled back. That's how sequences work in all DBMS (Oracle, PostgreSQL, Firebird, DB2, ...). Don't worry about "wasted" values or gaps in your generated IDs. Sep 8, 2012 at 15:59

2 Answers 2

2

Declare a table that stores all sequence numbers and then use statement like given below:

declare @SequenceNo int

    UPDATE MySequenceNumberTable
    SET @SequenceNo = SequenceNo = SequenceNo + 1
    WHERE SequenceName = 'Your sequence name'

Here single statement increments value, assigns to variable and updates DB table so its atomic action is guaranteed. Since its a simple DB update operation it plays part within DB transaction. If transaction is rolled back then SequenceNo field value is reverted back to its earlier value.

Only catch is next transaction requiring next value for the same sequence name must wait till the first transaction is either committed or rolled back because of row level locking. In case of DB sequence there is no locking of sequence so it never blocks other transactions requiring next value.

In our case we simply cannot have holes and need concurrency protection so we use above method and its working fine for us.

1
  • 1
    You have to be very careful with sequence tables. They can become a major bottleneck in high-volume systems and if you're careless about how you retrieve & update the values, you can get duplicates in your calling application(s).
    – alroc
    Sep 6, 2015 at 21:00
-4

The ROLLBACK statement is used in the following manner to cancel an entire transaction:

BEGIN TRANSACTION
...
SQL Statement(s)

...
ROLLBACK TRANSACTION

The ROLLBACK command can also be used to cancel part of a transaction in the following manner:

BEGIN TRANSACTION
...
SQL Statement(s)

SAVE TRANSACTION savepoint_name

SQL Statement(s)

ROLLBACK TRANSACTION savepoint_name
1
  • Not entirely sure you understood the question Oct 5, 2012 at 20:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.