22,209
社区成员
发帖
与我相关
我的任务
分享
create table tb (id int identity(1,1),id2 char(2) ,id3 char(5))
insert tb select 'A','safs'
insert tb select 'B','dafs'
insert tb select 'C',Null
insert tb select 'C','ASS'
go
select * from tb
go
create trigger trigger_insert
on tb
for insert
as
begin
update a
set ID3=(select min(ID3) from tb where ID2=b.ID2)
from tb a
inner join INSERTED b on a.ID=b.ID
end
go
insert tb select 'C',null
select * from tb
/*
id id2 id3
----------- ---- -----
1 A safs
2 B dafs
3 C NULL
4 C ASS
5 C ASS
*/
drop table tb
update 表
set ID3=(select min(ID3) from 表 where ID2=b.ID2)
from 表 a
inner join INSERTED b on a.ID=b.ID