title: How to write a cursor inside a stored procedure in SQL Server 2008
body: <p>I have two tables in my database </p>
  
  <p><strong>Coupon Table</strong> </p>
  
  <ul>
  <li>id (int)</li>
  <li>Name (nvarchar(max))</li>
  <li>NoofUses (int)</li>
  </ul>
  
  <p><strong>CouponUse Table</strong></p>
  
  <ul>
  <li>id(int)</li>
  <li>Couponid(int)</li>
  <li>CreateDate(datetime)</li>
  </ul>
  
  <p>Whenever a user clicks on a coupon an entry goes into the <code>CouponUse</code> table containing that Coupon's id </p>
  
  <p>Now there is a column in the <code>coupon</code> table called <code>NoofUses</code>. I want to write a cursor inside a stored procedure which loops over <code>couponuse</code> table and sees how many rows are there for one coupon and fill that number in <code>NoofUses</code> field in coupon.</p>
  
  <p>I have this query</p>
  
  <pre><code>  select COUNT(*) as totalcount , Name as name from Coupon as coupon 
    join CouponUse as couponuse on coupon.id = couponuse.couponid
    group by couponuse.couponid , coupon.Name
  </code></pre>
  
  <p>which gives me the coupon name and its count from <code>couponuse</code>  </p>
  
  <p>But I don't know how to implement that in a stored procedure using a cursor  ?  </p>
  
  <p>Anything you ask about question will be appreciated , Thanks</p>