I am trying to achieve a task in SQL Server. I'm sharing the sample problem as I couldn't share the entire task description.
Problem: we have a table called Person
as follows:
Person_Id Person_Name Person_Age
--------- ----------- ----------
1 AAA 25
2 BBB 25
3 CCC 25
4 DDD 25
From that table, I want to use the Person_Id = 4
that is going to be kept inside a TRANSACTION
.
Person_Id Person_Name Person_Age
--------- ----------- ----------
4 DDD 25
While performing the above transaction, user wants to access (INSERT, UPDATE, DELETE
) all the other records (other than Person_Id = 4
) which are in the table as below:
Person_Id Person_Name Person_Age
--------- ----------- ----------
1 AAA 25
2 BBB 25
3 CCC 25
What I tried:
I tried with NOLOCK, ROWLOCK
but I couldn't achieve this. Kindly help me to achieve this scenario. I have also tried this link. As per this link, using
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
the SELECT
query is fetching the Unmodified data. For example, If I am trying to UPDATE
the record in a TRANSACTION
and the record got updated but the TRANSACTION
is busy with executing other statements.
Person_Id Person_Name Person_Age
--------- ----------- ----------
4 DDD 25
Now, when other connections are trying to SELECT
the records in the table, then all other records along with the Record: Person_Id = 4
(With old value) will get returned.
SERIALIZABLE Specifies the following:
Statements cannot read data that has been modified but not yet committed by other transactions.
From the above, When I am using SERIALIZABLE
isolation, it still returns the OLDER Record of Person_Id = 4
. This I don't want in this case.
I want to get all the other records, other than the records in a TRANSACTION
.
In other words, If a record(s) is locked in a TRANSACTION
, then that record(s) should not appear in any other SELECT STATEMENT EXECUTION
with different connections.