2020-12-07 11.15.sql 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. USE [befri_Goldhoo]
  2. GO
  3. /****** Object: Trigger [clearance].[tri_ClearanceAccDetail_INSERT] Script Date: 2020/12/7 10:10:41 ******/
  4. SET ANSI_NULLS ON
  5. GO
  6. SET QUOTED_IDENTIFIER ON
  7. GO
  8. CREATE TRIGGER Discount.[tri_DiscountAccDetail_INSERT] ON Discount.DiscountAccDetail
  9. FOR INSERT
  10. AS
  11. BEGIN
  12. SET NOCOUNT ON;
  13. -- =============================================
  14. -- 开发人员: You
  15. -- 开发日期: 20201207
  16. -- 功能描述: 当插入记录时自动更新折扣总帐
  17. -- =============================================
  18. MERGE Discount.DiscountAcc AS t
  19. USING INSERTED AS s
  20. ON ( s.WarehouseId = t.WarehouseId
  21. AND s.InventoryId = t.InventoryId
  22. )
  23. WHEN MATCHED
  24. THEN
  25. UPDATE
  26. SET Quantity = Quantity + s.IncreasedQty - s.DecreasedQty ,
  27. [DiscountCoef] = CASE WHEN s.DecreasedQty > 0
  28. THEN t.[DiscountCoef]
  29. ELSE s.[DiscountCoef]
  30. END
  31. WHEN NOT MATCHED
  32. THEN
  33. INSERT (
  34. WarehouseId ,
  35. InventoryId ,
  36. Quantity ,
  37. [DiscountCoef]
  38. )
  39. VALUES
  40. ( s.WarehouseId ,
  41. s.InventoryId ,
  42. s.IncreasedQty - s.DecreasedQty ,
  43. s.DiscountCoef
  44. );
  45. END