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:
-
--drop table tb
-
-
create
table tb
-
(字段
1
int, 字段
2
int, 字段
3
int, 字段
4
int)
-
-
-
drop
trigger trigger_tb
-
-
create
trigger dbo.trigger_tb
-
on tb
-
for
insert
-
as
-
-
;with t
-
as
-
(
-
select tb.*,
-
row_number()
over(
partition
by tb.字段
1
order
by tb.字段
4)
as
rownum
-
from tb
-
inner
join inserted i
-
on tb.字段
1 = i.字段
1
-
and tb.字段
3 = i.字段
3
-
and tb.字段
4 = i.字段
4
-
)
-
-
update t
-
set 字段
2 = 字段
1+
rownum
-
-
go
-
-
-
insert
into tb
-
select
101,
null,
1,
1
-
union all
select
102,
null,
1,
2
-
union all
select
101,
null,
1,
3
-
union all
select
102,
null,
1,
4
-
-
-
--查询
-
select *
-
from tb
-
/*
-
字段1 字段2 字段3 字段4
-
101 102 1 1
-
102 103 1 2
-
101 103 1 3
-
102 104 1 4
-
*/
Method 2 for 2000:
-
--drop table tb
-
-
create
table tb
-
(字段
1
int, 字段
2
int, 字段
3
int, 字段
4
int)
-
-
-
--drop trigger trigger_tb
-
-
create
trigger dbo.trigger_tb
-
on tb
-
for
insert
-
as
-
-
update tb
-
set 字段
2 = t1.字段
1 + (
select
count(*)
from tb t2
-
where t2.字段
1 = t1.字段
1
-
and t2.字段
4 <= t1.字段
4)
-
from tb t1
-
inner
join inserted i
-
on t1.字段
1 = i.字段
1
-
and t1.字段
3 = i.字段
3
-
and t1.字段
4 = i.字段
4
-
-
go
-
-
-
insert
into tb
-
select
101,
null,
1,
1
-
union all
select
102,
null,
1,
2
-
union all
select
101,
null,
1,
3
-
union all
select
102,
null,
1,
4
-
-
-
--查询
-
select *
-
from tb
-
/*
-
字段1 字段2 字段3 字段4
-
101 102 1 1
-
102 103 1 2
-
101 103 1 3
-
102 104 1 4
-
*/
As another example, SQL Server2000 update a table of the flip-flop to achieve:
-
--drop table mocta
-
-
create
table purtb
-
(请购单号
varchar(
10),参考单号
varchar(
10),备注
varchar(
50))
-
-
create
table mocta
-
(工单单号
varchar(
10),订单单号
varchar(
10))
-
-
-
insert
into purtb
-
select
'101',
'201',
'301'
union all
-
select
'102',
'302',
'302'
union all
-
select
'103',
'备料',
'备料'
-
-
insert
into mocta
-
select
'201',
'301'
union all
-
select
'202',
'302'
-
go
-
-
--drop trigger trigger_purtb_insert
-
-
-
-
create
trigger dbo.trigger_purtb_insert
-
on purtb
-
for
insert
-
as
-
-
update purtb
-
set 备注 =
isnull((
select t1.订单单号
-
from mocta t1
-
where i.参考单号 = t1.工单单号),
-
i.参考单号)
-
from inserted i
-
where purtb.请购单号 = i.请购单号
and
-
purtb.参考单号 = i.参考单号
-
-
-
go
-
-
-
insert
into purtb(请购单号,参考单号)
-
select
'104',
'201'
-
union all
select
'105',
'xxx'
-
-
-
-
-
--查询
-
select *
-
from purtb
-
/*
-
请购单号 参考单号 备注
-
101 201 301
-
102 302 301
-
103 备料 备料
-
104 201 301
-
105 xxx xxx
-
*/