SQL Server 2016 SP2
This is the logic that I am trying to implement. (Please don't mind the incomplete syntax)
CREATE PROCEDURE ProcName
BEGIN TRANSACTION [TRAN1]
UPDATE tbl -- Update 1 - This takes around 30 seconds
SET col1 = 1, col2 = b
WHERE ...
COMMIT TRANSACTION [TRAN1]
BEGIN TRANSACTION [TRAN2]
UPDATE tbl -- Update 2 - This takes around 2 minutes
SET col3 = d, col4 = e
WHERE ....
COMMIT TRANSACTION [TRAN2]
Over here, my main aim is to get the col1
and col2
updated first, and then the SP can take its time to update col3
and col4
.
However, I noticed that the stored procedure executes the entire script in 1 single block and data in all the 4 columns appear together after more than 2 minutes. I just keep refreshing the SELECT
to check for the data while the stored procedure is executing.
When I use SELECT * FROM tbl with (nolock)
, the data in col1
and col2
appears in around 30 seconds as expected and then the data in col3
and col4
appear in about 2 minutes.
Looking at this, it seems like the data from the 1st UPDATE
is not really committed when TRAN1
is committed.
We cannot use batch separators (GO
) in a SP so I thought the transactions could work. I'm not sure why it's not committing the rows.
Any inputs on this would be appreciated.
NOTE: the stored procedure is called at the end of a Python script. I feel the only other way to achieve what I need if the transactions don't work would be to split the 2nd UPDATE
in another stored procedure and call that stored procedure after the 1st stored procedure call from python.