Can someone explain if a While Loop with a RowCounter is faster or slower than using ROW_NUMBER in SQL server?
Or is there no difference?
The existing stored procedure is too complex to put it all in a View and is very slow.. this is the While Loop, but how could it be done with ROW_NUMBER in Sql Server 2008 R2, and would there be any performance benefit?
DECLARE @StopRow AS INT
SELECT @StopRow = MAX(RowID)
FROM #Temp1
DECLARE @RowCounter AS INT
SET @RowCounter = 1
DECLARE @colID INT
WHILE (@RowCounter <= @StopRow)
BEGIN
SELECT @colID = colID
FROM #Temp1
WHERE colRowID = @RowCounter
IF (
EXISTS (
SELECT ParentColID
FROM ParentTable a WITH (NOLOCK)
JOIN MoreTableData b WITH (NOLOCK) ON a.priID = b.priID
AND colID = @colID
WHERE anotherID NOT IN (
SELECT anotherID
FROM @anotherTempTable
)
)
)
UPDATE #Temp1
SET aFlag = 0
WHERE colRowID = @RowCounter
SET @RowCounter = @RowCounter + 1
END
Found this StackOverflow now that I understand the SET based (UPDATE) verbage .. How to convert a loop in SQL to Set-based logic