0

Need explanation for the below query. While committing the outer transaction T , the row has been already deleted by nested transaction. But still the value with ID 2 is selected and displayed.

BEGIN TRAN T
SELECT * from tbl_types where ID=2

    BEGIN TRAN nested
       DELETE from tbl_types where ID=2
    COMMIT TRAN nested

COMMIT TRAN T
Lukasz Szozda
  • 139,860
  • 19
  • 198
  • 228
Vishwas Rao
  • 91
  • 1
  • 1
  • 10

1 Answers1

0

The result set from the first select has already been sent back to the client.

It doesn't matter what has happened since; the next query will find no matches for the since-deleted records.

This is simply related to statement ordering. The following, without any nested/explicit transactions, would produce the same results:

SELECT * from tbl_types where ID=2
DELETE from tbl_types where ID=2

While this would return no results:

DELETE from tbl_types where ID=2
SELECT * from tbl_types where ID=2
user2864740
  • 57,407
  • 13
  • 129
  • 202