现在的位置: 首页 > 综合 > 正文

SQL触发器

2017年12月19日 ⁄ 综合 ⁄ 共 1411字 ⁄ 字号 评论关闭

 

  1. 1. 创建一个触发器,当一本书被还回时,从LOAN表中删除相应的借阅记录,将该学生借阅这本书记录添加到LoadHist表中;并检查是否有用户在等待预约这本书,如有则将这本书的借阅状况修改为 已经预约;按照预约的日期先后,先向预约在前的用户发送信息,并将状态改为T。   
  2.  
  3. 2. 创建一个触发器,当借书成功,则检索是否有这个用户的预约该书的记录,如果有,则删除相应的预约记录。  
  4.  
  5. 3. 创建一个触发器,当读者还书时,如果超期罚款, 通过触发器在收费表中添加一条收费记录。  
  6.  
  7.    
  8.  
  9. 1.create trigger umgsai ON 
  10. loan                       --在Goods表中创建触发器   
  11. for delete 
  12. As                                        --事件触发后所要做的事情             
  13. begin   
  14. declare @借阅证号 char(20);  
  15. declare @借阅证号1 char(20)  
  16. declare @书号 char(20);  
  17. declare @借阅日期 char(20);  
  18. declare @归还日期 char(20);  
  19. declare @ISBN char(20);  
  20.  
  21. select @借阅证号=L.借阅证号 from deleted L;  
  22. select @书号=L.书号 from deleted L;  
  23. select @借阅日期=L.借阅日期 from deleted L;  
  24. select @ISBN =(select ISBN from books where 书号='@书号')  
  25. set @归还日期=getdate()   
  26. begin 
  27. insert into loanhist (借阅证号,书号,借阅日期,归还日期)values(@借阅证号,@书号,@借阅日期,@归还日期);  
  28. select @借阅证号1=(select top 1 借阅证号 from reservation where ISBN=@ISBN )  
  29. update reservation set 状态='T' where 借阅证号=@借阅证号1  
  30. end;  
  31. end;  
  32.  
  33.  2.  
  34.  
  35. create trigger umgsai1 ON 
  36. loan                       --在Goods表中创建触发器   
  37. for insert 
  38. As                                        --事件触发后所要做的事情             
  39. begin   
  40. declare @借阅证号 char(20);  
  41. declare @书号 char(20);  
  42. declare @ISBN char(20);  
  43. select @借阅证号=L.借阅证号 from inserted L  
  44.  
  45. select @书号=L.书号 from inserted L  
  46. select @ISBN=(select ISBN from books where 书号=@书号)  
  47. begin 
  48. delete from reservation where 借阅证号=@借阅证号 and ISBN= @ISBN  
  49. end;  
  50. end;  

 

本文出自 “阿凡达” 博客,请务必保留此出处http://shamrock.blog.51cto.com/2079212/702562

抱歉!评论已关闭.