I will explain my scenario with an example.
I have multiple rows in my table. I am picking those one by one for processing. I need to lock the row for processing.
Sample code looks like,
select top 1 * into #open_order from orders with (xlock)
where status = 'open' order by order_time;
EDIT: Added order by clause in the query.
My requirement is to run this in parallel connections. My problem here is, I cannot run this code on multiple connection in parallel. The second one waits until the first one commit the transaction.
Is there any way to exclude already locked rows from this select query?
I have come across with(readpast)
. But I don't know whether it can be used together or not.
EDIT: Sample data and expectation.
Orders table data:
id, order_time, status, remark
1, 2019-01-01 00:00:01, 'open', 'Sample 1'
2, 2019-01-02 00:00:01, 'open', 'Sample 2'
3, 2019-01-03 00:00:01, 'open', 'Sample 1'
If first row is locked, I am expecting to get the second row as result of the query.