表Ta
字段:C1 是外键,关联父表Tb
字段:c2 是整形类型,存储金额有个触发器,当插入一条记录到Ta的时候,会统计一下同一Tb下的c2的合计如:
Tac1 c2
-------
1 10
1 15如果插入 记录 insert into ta values(1,20)
触发器里面取值:
declare @c2 float,@receiveID varchar(32)
select @receiveID=c1 from insertedselect @c2=isnull(sum(c2),0) from ta where c1 in (select c1 from inserted)
select @c2
--将得到 20select @c2=isnull(sum(fF0),0) from udfobjrkyub where lF1 in (@receiveID)
select @c2
--将得到 45
求解~~~~~
字段:C1 是外键,关联父表Tb
字段:c2 是整形类型,存储金额有个触发器,当插入一条记录到Ta的时候,会统计一下同一Tb下的c2的合计如:
Tac1 c2
-------
1 10
1 15如果插入 记录 insert into ta values(1,20)
触发器里面取值:
declare @c2 float,@receiveID varchar(32)
select @receiveID=c1 from insertedselect @c2=isnull(sum(c2),0) from ta where c1 in (select c1 from inserted)
select @c2
--将得到 20select @c2=isnull(sum(fF0),0) from udfobjrkyub where lF1 in (@receiveID)
select @c2
--将得到 45
求解~~~~~
Go
if object_id('a') Is Not Null
Drop Table a
if object_id('b') Is Not Null
Drop Table b
Go
Create Table b
(
c1 int Primary key,
c2 int
)Create Table a
(
c1 int foreign key references b(c1),
c2 int
)
Go
--觸發器
If exists(Select 1 From sys.triggers Where name='tr_a')
Drop trigger tr_a
Go
Create trigger tr_a On a After Insert
As
Update b
Set b.c2=Isnull(c.sumC2,0)
From b
cross apply(Select sum(c2)As sumC2 From a Where a.c1=b.c1) As c
Where Exists(Select 1 From inserted Where c1=b.c1)
Go
--test
Insert Into b(c1,c2) values(1,0)
Insert Into b(c1,c2) values(2,0)Go
--Insert前
Select * From b
Go
Insert Into a(c1,c2) values(1,10)
Insert Into a(c1,c2) values(1,20)
Insert Into a(c1,c2) values(1,20)
Insert Into a(c1,c2) values(2,10)
Insert Into a(c1,c2) values(2,20)
Go
--Insert后
Select * From b