在论坛中出现的比较难的sql问题:9(触发器专题 插入数据自动更新表数据)

原文: 在论坛中出现的比较难的sql问题:9(触发器专题 插入数据自动更新表数据)

最近,在论坛中,遇到了不少比较难的sql问题,虽然自己都能解决,但发现过几天后,就记不起来了,也忘记解决的方法了。

所以,觉得有必要记录下来,这样以后再次碰到这类问题,也能从中获取解答的思路。


触发器问题,插入数据时,自动更新表的数据

http://bbs.csdn.net/topics/390634682

表1有字段1,字段2
插入数据4行
字段1   字段2
101
102
101
102
我想通过触发器,直接更新字段2,实现
字段1   字段2
101     101+1
102     102+1
101     101+2
102     102+2
这样的功能,请各位帮忙看看,如何来实现,

在插入数据的时候,实现更新。


方法1,适合2005及以后的版本:


   
   
  1. --drop table tb
  2. create table tb
  3. (字段 1 int, 字段 2 int, 字段 3 int, 字段 4 int)
  4. drop trigger trigger_tb
  5. create trigger dbo.trigger_tb
  6. on tb
  7. for insert
  8. as
  9. ;with t
  10. as
  11. (
  12. select tb.*,
  13. row_number() over( partition by tb.字段 1 order by tb.字段 4) as rownum
  14. from tb
  15. inner join inserted i
  16. on tb.字段 1 = i.字段 1
  17. and tb.字段 3 = i.字段 3
  18. and tb.字段 4 = i.字段 4
  19. )
  20. update t
  21. set 字段 2 = 字段 1+ rownum
  22. go
  23. insert into tb
  24. select 101, null, 1, 1
  25. union all select 102, null, 1, 2
  26. union all select 101, null, 1, 3
  27. union all select 102, null, 1, 4
  28. --查询
  29. select *
  30. from tb
  31. /*
  32. 字段1 字段2 字段3 字段4
  33. 101 102 1 1
  34. 102 103 1 2
  35. 101 103 1 3
  36. 102 104 1 4
  37. */

方法2,适合2000:


   
   
  1. --drop table tb
  2. create table tb
  3. (字段 1 int, 字段 2 int, 字段 3 int, 字段 4 int)
  4. --drop trigger trigger_tb
  5. create trigger dbo.trigger_tb
  6. on tb
  7. for insert
  8. as
  9. update tb
  10. set 字段 2 = t1.字段 1 + ( select count(*) from tb t2
  11. where t2.字段 1 = t1.字段 1
  12. and t2.字段 4 <= t1.字段 4)
  13. from tb t1
  14. inner join inserted i
  15. on t1.字段 1 = i.字段 1
  16. and t1.字段 3 = i.字段 3
  17. and t1.字段 4 = i.字段 4
  18. go
  19. insert into tb
  20. select 101, null, 1, 1
  21. union all select 102, null, 1, 2
  22. union all select 101, null, 1, 3
  23. union all select 102, null, 1, 4
  24. --查询
  25. select *
  26. from tb
  27. /*
  28. 字段1 字段2 字段3 字段4
  29. 101 102 1 1
  30. 102 103 1 2
  31. 101 103 1 3
  32. 102 104 1 4
  33. */


另一个例子,SQL Server2000触发器实现一个表的更新:


   
   
  1. --drop table mocta
  2. create table purtb
  3. (请购单号 varchar( 10),参考单号 varchar( 10),备注 varchar( 50))
  4. create table mocta
  5. (工单单号 varchar( 10),订单单号 varchar( 10))
  6. insert into purtb
  7. select '101', '201', '301' union all
  8. select '102', '302', '302' union all
  9. select '103', '备料', '备料'
  10. insert into mocta
  11. select '201', '301' union all
  12. select '202', '302'
  13. go
  14. --drop trigger trigger_purtb_insert
  15. create trigger dbo.trigger_purtb_insert
  16. on purtb
  17. for insert
  18. as
  19. update purtb
  20. set 备注 = isnull(( select t1.订单单号
  21. from mocta t1
  22. where i.参考单号 = t1.工单单号),
  23. i.参考单号)
  24. from inserted i
  25. where purtb.请购单号 = i.请购单号 and
  26. purtb.参考单号 = i.参考单号
  27. go
  28. insert into purtb(请购单号,参考单号)
  29. select '104', '201'
  30. union all select '105', 'xxx'
  31. --查询
  32. select *
  33. from purtb
  34. /*
  35. 请购单号 参考单号 备注
  36. 101 201 301
  37. 102 302 301
  38. 103 备料 备料
  39. 104 201 301
  40. 105 xxx xxx
  41. */


发布了416 篇原创文章 · 获赞 135 · 访问量 94万+

猜你喜欢

转载自www.cnblogs.com/lonelyxmas/p/12019990.html
今日推荐