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
  • 162,964
  • 23
  • 234
  • 275
Vishwas Rao
  • 121
  • 1
  • 1
  • 11

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
  • 60,010
  • 15
  • 145
  • 220