I have a stored procedure which looks like this:
USE [DBName]
GO
/****** Object: StoredProcedure [dbo].[ProcName] Script Date: 10/03/2012 12:10:16 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[ProcName](@filter bigint, @n int)
AS
DELETE FROM TableName WITH (READPAST, UPDLOCK, ROWLOCK) OUTPUT(DELETED.ColumnName2)
WHERE TableName.ID in (select top (@n) ID from TableName where TableName.ColumnName1 = @filter)
This procedure returns the first @n values of ColumnName2 (the records represented by these values will be deleted from the table). I am using this stored procedure from a vb method and it usually works well. However, sometimes, for reasons not known by me it throws an exception:
You can only specify the READPAST lock in the READ COMMITTED or REPEATABLE READ isolation levels.
My vb method calls this stored procedure and if it doesn’t get enough values, then it generates new values and calls this stored procedure. This is repeated until there are enough values. In essence, TableName works like a queue and my vb method works well, however, sometimes the exception mentioned above is thrown. What can cause this and what is the solution to my problem?
I have tried to initiate a new connection just for the code which calls my stored procedure, but to no avail, as the exception was thrown again. I have no idea what could be the solution, in spite the fact that I have read the following articles:
http://www.red-gate.com/messageboard/viewtopic.php?t=13614
.NET READPAST lock error when calling a stored procedure
http://support.microsoft.com/kb/981995
Thank you in advance for any help,
Lajos Árpád.
This should solve your problem
Since you are using READPAST, which requires this setting, you might as well set it explicitly in the SP, given that it is safely scoped.
Reference: SET TRANSACTION ISOLATION LEVEL
As for why it is happening, a likely reason is connection pooling and mixed transaction isolation levels between SQL calls.