gkid xmmc dj xj
1234 服务费 100 100
1234 餐费 60 60
提:用insert 触发器实现,当表'xfmx'中插入一条xmmc='餐费'时, 相同gkid下xmmc='服务费'更新为dj='99',xj='99'
用insert触发器怎么写,请各位高手相助
create trigger insert_trg on xfmx for insert as declare @a varchar(100) ,@b varchar(100)
,@c varchar(100)
,@d varchar(100)
select @a=gkid,@b=xmmc,@c=dj,@d=xj from inserted
if(@b='餐费')
begin insert into xfmx(gkid,xmmc,dj,xj)values(@a,'服务费',99,99)
end GO
可能我之前没表述清楚,第一条服务费的记录是默认有的,餐费是后面加上去的,gkid系统会自动同步的,因为他是同一个顾客消费的(所以这里不用更新gkid)
要求:当录入消费且xmmc='餐费'时,把xmmc='服务费'的dj和xj更新为99,
更新的判断条件是(select gkid from xfmx where xmmc='服务费')=(select gkid from xfmx where xmmc='餐费')
建议不用触发器.写逻辑代码即可
CREATE TRIGGER tri_xfmx AFTER INSERT ON xfmx FOR EACH ROW BEGIN UPDATE xfmx SET dj='99',xj='99' WHERE gkid=new.gkid AND xmmc='服务费'; END
参考博客 http://www.cnblogs.com/CraryPrimitiveMan/p/4206942.html
没执行过去,第一行有误
@wodao: 你的数据库是?
@苍枫露雨: 2008 r2
@wodao:
好吧,我以为是mysql呢,
CREATE TRIGGER tr_temp on xfmx
FOR insert
AS
BEGIN
declare @gkid varchar(20)=''
declare @xmmc varchar(20)=''
select @gkid=gkid,@xmmc=xmmc from inserted
if @xmmc='餐费'
begin
update xfmx
set dj=99,xj=99
where gkid=@gkid
and xmmc='服务费'
end
END
GO