0

我有一张桌子tblBillingtblTotalFee。我在tblBilling中的一列名为RemainingAmount,在tblTotalFee我有另一列名为Due From Previous Month。现在我想要的是当我在Remaining Amount中插入一个值时,我希望该值自动插入到Due From Previous Month中。我正在尝试编写一个触发器。但我就是做错了??谁能帮我??

我试过:

ALTER trigger [dbo].[trg_Billing_TotalFee] on [dbo].[tblBilling] 
after insert as 
insert into tblTotalFee(DueFromPreviousMonth) 
select RemainingAmount from inserted
4

1 回答 1

1

给你一个例子:


create table tblBilling (ID int identity(1000,1) primary key,
                         RemainingAmount int 
                         )
go
create table tblTotalFee (ID int identity(1000, 1) primary key,
                          DueFromPreviousMongh int)
go 
create trigger tr_tblBillingSync on tblBilling 
after insert 
as 
    insert into tblTotalFee (DueFromPreviousMongh)
    select RemainingAmount from inserted
go 
insert into tblBilling 
select 25
union all select 27
union all select 33
go
select * from tblBilling
select * from tblTotalFee
go 

最终输出结果:


ID     |  RemainingAmount
-------------------------
1000   |    25
1001   |    27
1002   |    33

ID     |  DueFromPreviousMongh
-------------------------
1000   |    25
1001   |    27
1002   |    33
于 2013-04-05T08:38:12.983 回答