dindansp取件ok触发器.sql 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. --insert类型触发器;
  2. if (object_id('tgr_dindansp_insert','tr') is not null)
  3. drop trigger tgr_dindansp_insert
  4. go
  5. create trigger tgr_dindansp_insert on dindansp for insert--订单表更新数据时;
  6. as
  7. begin transaction
  8. declare @order nvarchar(24) --订单号;
  9. declare @spid nvarchar(64) --商品id;
  10. --获取update表更新的dindansp数据;
  11. select @order = id, @spid = spid from inserted
  12. if (@spid is not null and @spid <> '')
  13. update dindan set [status3] = '未取' where [id] = @order
  14. commit transaction
  15. go
  16. --update类型触发器;
  17. if (object_id('tgr_dindansp_update','tr') is not null)
  18. drop trigger tgr_dindansp_update
  19. go
  20. create trigger tgr_dindansp_update on dindansp for update--订单表更新数据时;
  21. as
  22. begin transaction
  23. declare @order nvarchar(24) --订单号;
  24. declare @countAll int --订单所有产品数量;
  25. declare @countOK int --订单取件OK的产品数量;
  26. --获取update表更新的dindansp数据;
  27. select @order = id from inserted
  28. select @countAll=count(*) from dindansp where [id] = @order
  29. select @countOK=count(*) from dindansp where [id] = @order and [status2] = 'OK' and ( spid is not null and spid <> '')
  30. if ( @countAll = @countOK )
  31. update dindan set [status3] = 'OK' where [id] = @order
  32. else
  33. update dindan set [status3] = '未取' where [id] = @order
  34. commit transaction
  35. go
  36. --delete类型触发器;
  37. if (object_id('tgr_dindansp_delete','tr') is not null)
  38. drop trigger tgr_dindansp_delete
  39. go
  40. create trigger tgr_dindansp_delete on dindansp for delete--订单表更新数据时;
  41. as
  42. begin transaction
  43. declare @order nvarchar(24) --订单号;
  44. declare @countAll int --订单所有产品数量;
  45. declare @countOK int --订单取件OK的产品数量;
  46. --获取update表更新的dindansp数据;
  47. select @order = id from deleted
  48. select @countAll=count(*) from dindansp where [id] = @order
  49. select @countOK=count(*) from dindansp where [id] = @order and [status2] = 'OK' and ( spid is not null and spid <> '')
  50. if ( @countAll = @countOK )
  51. update dindan set [status3] = 'OK' where [id] = @order
  52. commit transaction
  53. go
  54. --更新dindansp表,使之更新dindan表;
  55. select identity(int,1,1) as aid, autoid, zs into #temp1 from dindansp
  56. declare @maxid int
  57. declare @id nvarchar(50)
  58. declare @zs nvarchar(20)
  59. select @maxid = max(aid) from #temp1
  60. while @maxid > 0
  61. begin
  62. select @id = autoid, @zs = zs from #temp1 where aid = @maxid
  63. update dindansp set zs = @zs where autoid = @id
  64. set @maxid = @maxid -1
  65. end
  66. drop table #temp1