How to write a cursor inside a stored procedure in SQL Server 2008

前端 未结 3 1113
栀梦
栀梦 2021-02-20 06:28

I have two tables in my database

Coupon Table

  • id (int)
  • Name (nvarchar(max))
  • NoofUses (int)

Coup

3条回答
  •  甜味超标
    2021-02-20 06:54

    What's wrong with just simply using a single, simple UPDATE statement??

    UPDATE dbo.Coupon
    SET NoofUses = (SELECT COUNT(*) FROM dbo.CouponUse WHERE Couponid = dbo.Coupon.ID)
    

    That's all that's needed ! No messy and complicated cursor, no looping, no RBAR (row-by-agonizing-row) processing ..... just a nice, simple, clean set-based SQL statement.

提交回复
热议问题