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

前端 未结 3 1112
栀梦
栀梦 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:49

    You can create a trigger which updates NoofUses column in Coupon table whenever couponid is used in CouponUse table

    query :

    CREATE TRIGGER [dbo].[couponcount] ON [dbo].[couponuse]
    FOR INSERT
    AS
    if EXISTS (SELECT 1 FROM Inserted)
      BEGIN
    UPDATE dbo.Coupon
    SET NoofUses = (SELECT COUNT(*) FROM dbo.CouponUse WHERE Couponid = dbo.Coupon.ID)
    end 
    

提交回复
热议问题