Skip to content
Advertisement

How to remove While loop inside stored procedure in MS SQL Server? [closed]

I have a stored procedure with a cursor inside for the logic which is taking long time for execution . I have changed to while loop for faster execution but Even while loop takes so long time for the below query . Please help me to change a while loop for faster execution . I have updated the description for better understanding .

enter image description here

Advertisement

Answer

Could be wrong, but looks like a “gaps and islands” problem.

  • We first check whether each row has the same ListType as the previous.

  • Then we create a Group number for each new stretch of values.

  • Then we get a rownumber partitioned on this grouping.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement