title: Alternative to use cursors in SQL Server stored procedure
body: <p>It's not like that I am having trouble executing my cursors which are enclosed in a stored procedure. But I want to find more efficient way to achieve the same.</p>
  
  <p>Here it goes.</p>
  
  <p>Stored procedure : <code>RawFeed.sql</code> (runs every 5 minutes)</p>
  
  <pre><code>Set @GetATM = Cursor For
          Select DeviceCode,ReceivedOn
          From RawStatusFeed
          Where CRWR=2 AND Processed=0
          Order By ReceivedOn Desc
      Open @GetATM
      Fetch Next
      From @GetATM Into @ATM,@ReceivedOn
      While @@FETCH_STATUS = 0
          Begin
              Set @RawFeed=@ATM+' '+Convert(VarChar,@ReceivedOn,121)+' '+'002307'+' '+@ATM+' : Card Reader/Writer - FAULTY '
              Exec usp_pushRawDataAndProcess 1,@RawFeed
              Fetch Next
              From @GetATM Into @ATM,@ReceivedOn
          End
  Set @GetATM = Cursor For
          Select DeviceCode,ReceivedOn
          From RawStatusFeed
          Where CRWR=0 AND Processed=0
          Order By ReceivedOn Desc
      Open @GetATM
      Fetch Next
      From @GetATM Into @ATM,@ReceivedOn
      While @@FETCH_STATUS = 0
          Begin
              Set @RawFeed=@ATM+' '+Convert(Varchar,@ReceivedOn,121)+' '+'002222'+' '+@ATM+' : Card Reader/Writer - OK '
              Exec usp_pushRawDataAndProcess 1,@RawFeed
              Fetch Next
              From @GetATM Into @ATM,@ReceivedOn
          End
  </code></pre>
  
  <p>Likewise I have 10 more <code>SET</code> statements which differ on <code>WHERE</code> condition parameter &amp; string enclosed in <code>@RawFeed</code> variable.</p>
  
  <p>For each row I get I execute another stored procedure on that particular row.</p>
  
  <p>My question is </p>
  
  <blockquote>
    <p><em>Is there any better way to achieve the same without using cursors?</em></p>
  </blockquote>
  
  <p>Variable @<code>RawFeed</code> Contains following <code>string</code> which is input to <code>usp_pushRawDataAndProcess</code> stored procedure. now this will divide whole string and do some operation like <code>INSERT,UPDATE,DELETE</code> on some tables.</p>
  
  <p>WE JUST CAN NOT PROCESS MORE THAN 1 STRING IN <code>usp_pushRawDataAndProcess</code> </p>
  
  <pre><code>NMAAO226 2012-09-22 16:10:06.123 002073 NMAAO226 : Journal Printer - OK 
  WMUAO485 2012-09-22 16:10:06.123 002222 WMUAO485 : Card Reader/Writer - OK 
  </code></pre>