I have a SP that updates prices in a table using a CURSOR and FETCH NEXT.
The cursor's query is a simple "select', and instead of a WHERE - I'm joining a function who brings all the related User IDs.
Now, due to the fact the my SP updates a lot of different tables (attached is only part of it), and in every update I use the JOIN of the users function - I wanted to save some time, and bring the Users list only once, at the beginning of the SP.
If I join my temp table (@t, which holds the User IDs) I get an infinite loop. At the end the prices will be infinite.
BUT - If I join the function itself, instead of the temp table that should have exactly the same values in it, everything is fine.
I commented the problematic JOIN.
-- This is my main object, who "holds" a lot of users
DECLARE @DistID INT = 123
DECLARE @t TABLE
(MainDistID int,
DistID int,
UserID int)
INSERT INTO @t
SELECT MainDistID,
DistID,
UserID
FROM [MyScheme].[FnGetAllDistAndUsers] (@DistID)
DECLARE @Bid INT -- Will contain the ID we need to update
DECLARE c CURSOR LOCAL FOR
SELECT ID
FROM BillingDetails AS bd
-- BOTH JOINS SHOULD BE THE SAME:
--JOIN @t AS GUsers -- Infinite loop...
-- ON bd.UserID = GUsers.UserID
JOIN [MyScheme].[FnGetAllDistAndUsers] (@DistID) AS GUsers -- NO infinite loop
ON bd.UserID = GUsers.UserID
OPEN c
FETCH NEXT FROM c
INTO @Bid
WHILE @@FETCH_STATUS = 0
BEGIN
UPDATE MyTable
SET Price = Price * 2
WHERE MyTableID = @Bid
FETCH NEXT FROM c
INTO @Bid
END
Thanks!