MSSQL之十七 触发器使用项目案例

举报
tea_year 发表于 2021/12/29 23:20:43 2021/12/29
【摘要】 触发器使用项目案例 1、添加一个商家系统自动增加一个商家帐号 -- 在商家表中设计Insert触发器CREATE TRIGGER t_business_InsertON shop_businesstbAFTER INSERTAS BEGIN DECLARE @businessname nvarchar(15)SELECT @busine...

  
  1. 触发器使用项目案例
  2. 1、添加一个商家系统自动增加一个商家帐号
  3. -- 在商家表中设计Insert触发器
  4. CREATE TRIGGER t_business_Insert
  5. ON shop_businesstb
  6. AFTER INSERT
  7. AS
  8. BEGIN
  9. DECLARE @businessname nvarchar(15)
  10. SELECT @businessname = businessname FROM Inserted
  11. print '恭喜您,添加了一个商家,商家名称:' + @businessname
  12. insert into dbo.shop_adminusertb
  13. (adminid,adminuser,pass,adminright,admintype,busid,admindepart,mobilephone)
  14. select busid,businessname,'1111','20',12,busid,businessname,contactmobile from Inserted
  15. END
  16. GO
  17. -- 在管理员表中设计Insert触发器
  18. Create TRIGGER t_adminusertb_Insert
  19. ON dbo.shop_adminusertb
  20. AFTER INSERT
  21. AS
  22. BEGIN
  23. DECLARE @adminid varchar(20),
  24. @adminuser varchar(50),
  25. @pass varchar(10),
  26. @adminright varchar(10)
  27. SELECT @adminid = adminid ,
  28. @adminuser = adminuser,
  29. @pass= pass,
  30. @adminright= adminright
  31. FROM Inserted
  32. print '添加了一个帐号'+ char(13) +
  33. '用户名称:' + @adminuser + char(13) +
  34. '用户名:' + @adminid + char(13) +
  35. '密 码:' + @pass + char(13) +
  36. '权 限:' + @adminright
  37. END
  38. 2、变更订单为订单失败,系统自动退还商品库存
  39. -- 变更订单
  40. Create TRIGGER t_order_updateState
  41. ON dbo.shop_shopcarttb
  42. FOR UPDATE
  43. AS
  44. BEGIN
  45. SET NOCOUNT ON
  46. if UPDATE (orderstate)
  47. BEGIN
  48. declare @orderstate int,@oldorderstate int
  49. set @orderstate = (select orderstate from inserted)
  50. set @oldorderstate = (select orderstate from deleted)
  51. print '变更订单状态:由 ' + cast(@oldorderstate as varchar(5)) + ' 变更为 ' + cast(@orderstate as varchar(5))
  52. if (@orderstate = 904)
  53. begin
  54. update dbo.shop_gift11tb
  55. set giftnum = giftnum + 1 , giftchangenum = giftchangenum + 1
  56. where giftid = (select giftid from inserted)
  57. end
  58. END
  59. SET NOCOUNT OFF
  60. END
  61. -- 商品库存变更
  62. Create TRIGGER t_gift_updateState
  63. ON dbo.shop_gift11tb
  64. FOR UPDATE
  65. AS
  66. BEGIN
  67. if UPDATE (giftnum)
  68. BEGIN
  69. declare @newNum int, @oldNum int, @giftid varchar(20)
  70. set @oldNum = (select giftnum from deleted)
  71. select @newNum = giftnum, @giftid = giftid from inserted
  72. print '变更商品(' + @giftid + ')库存,由 ' + cast(@oldNum as varchar(5)) + ' 变更为 ' + cast(@newNum as varchar(5))
  73. END
  74. END
  75. GO
  76. 3、删除一个商家,系统自动删除该商家的登录帐号
  77. -- 在商家表中设计Delete触发器
  78. Create TRIGGER t_business_Delete
  79. ON dbo.shop_businesstb
  80. FOR DELETE
  81. AS
  82. BEGIN
  83. SET NOCOUNT ON
  84. DECLARE @businessname nvarchar(15)
  85. SELECT @businessname = businessname FROM Deleted
  86. print '删除了' + CAST(@@RowCount as varchar(5)) + '个商家,商家名称:' + @businessname
  87. DELETE dbo.shop_adminusertb FROM dbo.shop_adminusertb
  88. INNER JOIN Deleted ON shop_adminusertb.adminid = Deleted.busid
  89. SET NOCOUNT OFF
  90. END
  91. Go
  92. -- 在管理员表中设计Delete触发器
  93. Create TRIGGER t_adminusertb_Delete
  94. ON dbo.shop_adminusertb
  95. AFTER DELETE
  96. AS
  97. BEGIN
  98. DECLARE @adminid varchar(20),
  99. @adminuser varchar(50),
  100. @pass varchar(10),
  101. @adminright varchar(10)
  102. SELECT @adminid = adminid ,
  103. @adminuser = adminuser,
  104. @pass= pass,
  105. @adminright= adminright
  106. FROM Deleted
  107. print '删除了' + CAST(@@RowCount as varchar(5)) + '个帐号'+ char(13) +
  108. '用户名称:' + @adminuser + char(13) +
  109. '用户名:' + @adminid + char(13) +
  110. '密 码:' + @pass + char(13) +
  111. '权 限:' + @adminright
  112. END
  113. GO

视频课 https://edu.csdn.net/course/play/7940


文章来源: aaaedu.blog.csdn.net,作者:tea_year,版权归原作者所有,如需转载,请联系作者。

原文链接:aaaedu.blog.csdn.net/article/details/52389433

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。