Sql more difficult problems in the forum: 9 (insert topic trigger automatic data update table data)

Original: more difficult problems in sql forum: 9 (insert topic trigger automatic data update table data)

Recently, in the forum, I met a lot more difficult sql problem, although they can be resolved, but found a few days later, they can not remember, forget the solution of.

So, I feel the need to be recorded, so that after the encounter this problem again, and get answers from the idea.


Trigger problem, insert the data, automatically updating data table

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

Table 1 has a field 1, field 2
is inserted into data of 4 rows
Field 1 Field 2
101
102
101
102
I want to trigger, direct update field 2, to achieve
Field 1 Field 2
101 101 + 1
102 102 + 1
101 + 101 2
102 2 102 +
this function, please help us to see how to achieve,

When inserting data, to achieve the update.


1, for 2005 and later:


   
   
  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. */

Method 2 for 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. */


As another example, SQL Server2000 update a table of the flip-flop to achieve:


   
   
  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. */


Published 416 original articles · won praise 135 · views 940 000 +

Guess you like

Origin www.cnblogs.com/lonelyxmas/p/12019990.html