think set based. You dont need cursor for this. i think as per your description, what you're looking for is thiscreate proc quantitybonus(@ppid int,@qin int,@qout int output)asbegin select p.*,p.qin + coalesce(db.bonus,0) as qtyincludingbonusfrom product pouter apply (select top 1 bonus from DrugsBonus where productid=p.productid and bonusamount<=p.qin order by bonusamount desc)dbwhere p.productid = @ppid--your required product idgo