When I remove the commented line, this works in about 3 secs. Adding the commented line (subquery) causes it to hang. When I run just the SELECT statement with the commented line in, it also takes about 3 secs.
So.. in summary, my code works as a select statement but breaks as a cursor.
Any ideas ?
DECLARE @FileId INT
DECLARE @ProcMsg VARCHAR(2000)
DECLARE @ErrCount INT
DECLARE @NumRec INT
SET @NumRec = 0
SET @ErrCount = 0
DECLARE LoopCur CURSOR READ_ONLY FOR
SELECT tf.FileId
FROM Table1 tf WITH (NOLOCK)
INNER JOIN Table2 tat WITH (NOLOCK) ON tf.AppointmentId = tat.[Id]
INNER JOIN Table3 tad WITH (NOLOCK) ON tat.AppointmentId = tad.[Id]
WHERE tf.ClientId = 199
AND tf.FileReceivedDate IS NULL
AND tf.CancelledDate IS NULL
AND tat.Status NOT IN ('a', 'c')
--AND NOT EXISTS (SELECT tac.FileId FROM Table4 tac WITH (NOLOCK) WHERE tac.TypeId = 234 AND tac.FileId = tf.FileId)
ORDER BY tad.[Date] + 6
OPEN LoopCur
FETCH NEXT FROM LoopCur INTO @FileId
WHILE @@FETCH_STATUS = 0
BEGIN
--EXEC SP HERE
SET @NumRec = @NumRec + 1
FETCH NEXT FROM LoopCur INTO @FileId
END
CLOSE LoopCur
DEALLOCATE LoopCur
PRINT @NumRec
Life is a blast when you have a semi-automatic..
So.. in summary, my code works as a select statement but breaks as a cursor.
Any ideas ?
DECLARE @FileId INT
DECLARE @ProcMsg VARCHAR(2000)
DECLARE @ErrCount INT
DECLARE @NumRec INT
SET @NumRec = 0
SET @ErrCount = 0
DECLARE LoopCur CURSOR READ_ONLY FOR
SELECT tf.FileId
FROM Table1 tf WITH (NOLOCK)
INNER JOIN Table2 tat WITH (NOLOCK) ON tf.AppointmentId = tat.[Id]
INNER JOIN Table3 tad WITH (NOLOCK) ON tat.AppointmentId = tad.[Id]
WHERE tf.ClientId = 199
AND tf.FileReceivedDate IS NULL
AND tf.CancelledDate IS NULL
AND tat.Status NOT IN ('a', 'c')
--AND NOT EXISTS (SELECT tac.FileId FROM Table4 tac WITH (NOLOCK) WHERE tac.TypeId = 234 AND tac.FileId = tf.FileId)
ORDER BY tad.[Date] + 6
OPEN LoopCur
FETCH NEXT FROM LoopCur INTO @FileId
WHILE @@FETCH_STATUS = 0
BEGIN
--EXEC SP HERE
SET @NumRec = @NumRec + 1
FETCH NEXT FROM LoopCur INTO @FileId
END
CLOSE LoopCur
DEALLOCATE LoopCur
PRINT @NumRec
Life is a blast when you have a semi-automatic..