订单收款触发器.sql 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. -- 创建收款触发器,当插入记录时自动添加实收款, 删除记录时自动减实收款;
  2. -- insert触发器;
  3. create trigger tgr_receipts_insert on [dindanbukuan] after insert as
  4. begin
  5. declare @order_number nvarchar(50) --订单号 id;
  6. declare @amount_received nvarchar(50) --实收款 money;
  7. declare @receipt_item nvarchar(50) --收款项目 item;
  8. declare @receipt_kind nvarchar(50) --收款类型 kind;
  9. declare @receipt_2_sales nvarchar(50) --收款二销 bz;
  10. declare @amount_old_received NVARCHAR(50) --旧的实收款;
  11. --获取值;
  12. select @order_number = id, @amount_received = [money], @receipt_item = item, @receipt_kind = kind, @receipt_2_sales = bz from inserted
  13. if ( @receipt_kind = '3' or @receipt_kind = '4') --订单收款;
  14. BEGIN
  15. --获取订单实收款;
  16. SELECT @amount_old_received = amount_received from dindan where id = @order_number
  17. --更新订单实收款;
  18. update dindan set amount_received = cast(cast(@amount_old_received as int) + cast(@amount_received as int) as nvarchar) where id = @order_number
  19. END
  20. ELSE --加挑收款;
  21. BEGIN
  22. --获取加挑实收款;
  23. SELECT @amount_old_received = amount_received from dindanbukuan2 WHERE id = @order_number and bz = @receipt_item
  24. --更新加挑实收款;
  25. update dindanbukuan2 set amount_received = cast(cast(@amount_old_received as int) + cast(@amount_received as int) as nvarchar) where id = @order_number and bz = @receipt_item
  26. END
  27. end
  28. go
  29. --delete触发器;
  30. create trigger tgr_receipts_delete on [dindanbukuan] after delete as
  31. begin
  32. declare @order_number nvarchar(50) --订单号 id;
  33. declare @amount_received nvarchar(50) --实收款 money;
  34. declare @receipt_item nvarchar(50) --收款项目 item;
  35. declare @receipt_kind nvarchar(50) --收款类型 kind;
  36. declare @receipt_2_sales nvarchar(50) --收款二销;
  37. declare @amount_old_received NVARCHAR(50) --旧的实收款;
  38. select @order_number = id, @amount_received = [money], @receipt_item = item, @receipt_kind = kind, @receipt_2_sales = bz from deleted
  39. if ( @receipt_kind = '3' or @receipt_kind = '4') --订单收款;
  40. BEGIN
  41. --获取订单实收款;
  42. SELECT @amount_old_received = amount_received from dindan where id = @order_number
  43. --更新订单实收款;
  44. update dindan set amount_received =cast(cast(@amount_old_received as int) - cast(@amount_received as int) as nvarchar) where id = @order_number
  45. END
  46. ELSE --加挑收款;
  47. BEGIN
  48. --获取加挑实收款;
  49. SELECT @amount_old_received = amount_received from dindanbukuan2 WHERE id = @order_number and bz = @receipt_item
  50. --更新加挑实收款;
  51. update dindanbukuan2 set amount_received = cast(cast(@amount_old_received as int) - cast(@amount_received as int) as nvarchar) where id = @order_number and bz = @receipt_item
  52. END
  53. end
  54. GO
  55. --没有更新update触发器;