| |

VerySource

 Forgot password?
 Register
Search
View: 904|Reply: 8

sql server update trigger

[Copy link]

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-1-27 19:40:01
| Show all posts |Read mode
There are two tables:
table1 (id, amount)
table2 (id, sum)

I want to get an update trigger. When table1.amount changes, table2.sum also changes.


How much table1.amount increases or decreases and how much table2.sum also increases or decreases
table1.amount is not equal to table2.sum
The accumulation of table1.amount is not table2.sum
Reply

Use magic Report

0

Threads

34

Posts

17.00

Credits

Newbie

Rank: 1

Credits
17.00

 China

Post time: 2020-2-22 11:15:02
| Show all posts
create trigger trigger name on table1
instead of update
as

   --update update
   declare @ old, @ new int
   if exists (select 1 from inserted) and exists (select 1 from deleted)
   begin
        set @ old = (select amount from deleted)
        set @ new = (select amount from inserted)
        update table2 set sum = sum + @ new- @ old where id = (selelct id from inserted)
   end

go
Reply

Use magic Report

0

Threads

49

Posts

35.00

Credits

Newbie

Rank: 1

Credits
35.00

 China

Post time: 2020-2-22 11:30:01
| Show all posts
create trigger t_update on table1
for update
as
begin
update t2 set t2.sum = t2.sum + t1.addamount
from table2 t2 inner join
(select a.id, b.amount-a.amount as addamount
 from deleted a inner join inserted b on a.id = b.id)
t1 on t1.id = t2.id
end
go
Reply

Use magic Report

0

Threads

34

Posts

17.00

Credits

Newbie

Rank: 1

Credits
17.00

 China

Post time: 2020-2-22 13:45:01
| Show all posts
table1.amount is not equal to table2.sum
The accumulation of table1.amount is not table2.sum
----------- ?? What is this for?
Reply

Use magic Report

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-2-22 18:30:02
| Show all posts
table1.amount is not equal to table2.sum
The accumulation of table1.amount is not table2.sum
----------- ?? What is this for?


The demand is like this, no reason, I try, thank you so much
Reply

Use magic Report

0

Threads

30

Posts

25.00

Credits

Newbie

Rank: 1

Credits
25.00

 United States

Post time: 2020-2-22 20:30:01
| Show all posts
create trigger t_1 on table1 for update
as
select inserted.ID, diff = deleted.amount- inserted.amount into #tmp
from deleted, inserted
where deleted.ID = inserted.ID

update table2
set sum = sum + # tmp.diff
from table2, # tmp
where table2.ID = # tmp.ID
go
Reply

Use magic Report

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-2-23 13:00:01
| Show all posts
Thank you all
Reply

Use magic Report

0

Threads

30

Posts

25.00

Credits

Newbie

Rank: 1

Credits
25.00

 China

Post time: 2020-2-23 14:45:01
| Show all posts
correct:
create trigger t_1 on table1 for update
as
select inserted.ID, diff = inserted.amount-deleted.amount into #tmp
from deleted, inserted
where deleted.ID = inserted.ID

update table2
set sum = sum + # tmp.diff
from table2, # tmp
where table2.ID = # tmp.ID
go
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-3-2 17:45:01
| Show all posts
Up, it turns out that instered and deleted tables have such benefits.
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list