title: How to have a loop inside SQL stored procedure
body: <p>Ok I have the below command which fetches me a set of data with some order number. The problem is when the set of data is more than one, the order value remains same instead of auto incrementing.</p>
  
  <p>For Example: </p>
  
  <p>If I fetch data one by one the <strong>Order</strong> value increment in the table as per the below logic, but if there is more than one data fetched at a time the order remains same i.e it auto increments for all the row fetched with same value. Maybe somekind of loop where I fetch data and update one by one can help me in this. Help me in implementing the looping/array condition in my stored procedure. My command are as follow:</p>
  
  <pre><code>declare @MaxOrder1 int=0
  
  select @MaxOrder1 = max([order]) from [dbo].[XYZ_ABC_Table1] where [Id]=@Id
  if (@MaxOrder1 is null)
  begin
  
  set @MaxOrder1 =0
  end
  set @MaxOrder1 = @MaxOrder1+1
  
  UPDATE STPC SET STPC.[IsIncluded]= PT.[Value] FROM [dbo].[XYZ_ABC_Table1] STPC  join @Table2 PT on (PT.Id = STPC.Id)  
     UPDATE STPC SET STPC.[Order]= case
                    when STPC.[IsIncluded] = 1 then @MaxOrder1
                    else ''
                    END
                    FROM [dbo].[XYZ_ABC_Table1] STPC  join @Table2 PT on (PT.Id = STPC.Id)
  </code></pre>