紧急(触发器问题)

yangq4000 2003-12-11 05:18:00
表t1 bh char, a,b float
bh a b
1001 1 1
1002 2 2
1001 2 2
1002 3 3
表t2
bh m [5%] [10%]
1001 a 0.15 0.3
1001 b 0.15 0.3
1002 a 0.25 0.5
1002 b 0.25 0.5
其中:
1001 m [5%]
0.15=sum(t1.a) where bh=1001
就是说,我想改变t1表中编号是1001的数据a,b的值,触发t2表中编号也是1001的数据的值,假如编号有很多的话,怎么写呢?谢谢
我想改变t1中的数值,达到t2那种效果,如何用触发器实现?谢谢
...全文
50 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
victorycyz 2003-12-12
  • 打赏
  • 举报
回复
记得你不久前已经发过一个相同问题的贴,只不过百分比是10%、20%、30%而已。不知道你对那贴中的别人的回复理解得怎样?我觉得如果有什么新的疑问,最好是根据回贴的内容展开讨论。别人才知道你的理解消化做到什么程度了。重复发贴,实在没什么意思。

再说一下我的意见,t2表是多余的,不需要。更不需要什么触发器来处理了。
DigJim 2003-12-11
  • 打赏
  • 举报
回复
上面的写错了!!

CREATE TRIGGER test ON t1
FOR update
AS
declare @sumA decimal(18,2)
declare @sumB decimal(18,2)

select @sumA=sum(t1.a),@sumB=sum(t1.b) from t1
where t1.bh=(select bh from inserted)

update t2 set [5%] =(sumA*0.05),[10%]=(sumA*0.25)
where t2.bh=(select bh from inserted) AND m='a'

update t2 set [5%] =(sumB*0.05),[10%]=(sumB*0.25)
where t2.bh=(select bh from inserted) AND m='b'
go
DigJim 2003-12-11
  • 打赏
  • 举报
回复
CREATE TRIGGER test ON t1
FOR update
AS
declare @sumA decimal(18,2)
declare @sumB decimal(18,2)

select @sumA=sum(t1.a),@sumB=sum(t1.b) from t1
where t1.bh=(select bh from inserted)

update t2 set [5%] =(sumA*0.05),[10%]=(sumB*0.25)
where t2.bh=(select bh from inserted)
go

shuiniu 2003-12-11
  • 打赏
  • 举报
回复
不需要表t2,用视图就可以了!
create table t1(bh char(4), a float,b float)
insert t1 values('1001',1,1)
insert t1 values('1002',2,2)
insert t1 values('1001',2,2)
insert t1 values('1002',3,3)

create view test
as
select bh,'a' m,sum(a) * 0.05 [5%],sum(b)*0.1 as [10%]
from t1
group by bh
union all
select bh,'b', sum(a) * 0.05 [5%],sum(b)*0.1 [10%]
from t1
group by bh
go

select * from test order by bh

34,587

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧