| |

VerySource

 Forgot password?
 Register
Search
View: 3053|Reply: 10

Looking for a simple trigger, I haven't written a trigger before, it should be very simple

[Copy link]

1

Threads

4

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-12-16 19:00:02
| Show all posts |Read mode
TA
id mtype mk
1 aa 123
2 bb 123

TB
id ntype Status ts
1 aa 0 123
2 bb 1 123
3 bb 0 10


----------------
Find a trigger, when mk in TA is updated, the corresponding ts in TB is also updated.

The condition for triggering TB is ntype=mtype and Status=0.

---
Such as operating on TA

update TA set mk='999' where mtype='bb'

Then TB should be:
TB
id ntype Status ts
1 aa 0 123
2 bb 1 123
3 bb 0 999

------------Find this trigger
Reply

Use magic Report

0

Threads

49

Posts

35.00

Credits

Newbie

Rank: 1

Credits
35.00

 China

Post time: 2020-12-16 19:15:01
| Show all posts
create trigger t_update on ta
for update
as
begin
update tb
set tb.ts = ta.mk
from inserted ta
where tb.ntype=ta.mtype and tb.Status = 0
end
go
Reply

Use magic Report

0

Threads

88

Posts

55.00

Credits

Newbie

Rank: 1

Credits
55.00

 China

Post time: 2020-12-16 20:45:01
| Show all posts
create trigger auto_update on TB
as
  update TB set ts = mk from inserted a where id = a.id and Status = 0
Reply

Use magic Report

0

Threads

6

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-12-16 21:00:01
| Show all posts
CREATE TRIGGER sync_update ON TA
FOR UPDATE
AS
if exists (select 1 from TB where ntype=INSERTED.mtype and Status=0)
    update TB set ts=INSERTED.mk
Reply

Use magic Report

0

Threads

11

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 China

Post time: 2020-12-16 21:45:01
| Show all posts
create trigger trigger_update on dbo.TA
for update
as
declare @type varchar(10)
declare @ts int
select @type=i.mytype,@ts=i.mk from inserted i
update TB set ts=@ts where ntype=@type and status=0
Reply

Use magic Report

1

Threads

4

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 Canada

 Author| Post time: 2020-12-18 13:15:02
| Show all posts
Thank you!
It's strange that I built a trigger according to the above method, why the trigger is always in the editing state (that is, a pen is displayed there). What can be used normally should be a green + sign!


Check the grammar and show success again. What is the reason?
Reply

Use magic Report

1

Threads

4

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 United States

 Author| Post time: 2020-12-18 15:00:01
| Show all posts
Why is that trigger always being edited

How to use it to use it?
Reply

Use magic Report

1

Threads

13

Posts

12.00

Credits

Newbie

Rank: 1

Credits
12.00

 China

Post time: 2020-12-19 13:00:02
| Show all posts
Use EXE to trigger
Reply

Use magic Report

1

Threads

13

Posts

12.00

Credits

Newbie

Rank: 1

Credits
12.00

 China

Post time: 2020-12-19 13:15:01
| Show all posts
Sorry, it's EXEC
Reply

Use magic Report

1

Threads

13

Posts

12.00

Credits

Newbie

Rank: 1

Credits
12.00

 China

Post time: 2020-12-19 13:30:01
| Show all posts
IF EXISTS (SELECT name FROM sysobjects
      WHERE name ='AA' AND type ='TR')
   DROP TRIGGER AA

CREATE TRIGGER AA
ON TA
FOR UPDATE
AS
UPDATE TB SET TB.TS=TA.MK
FROM INSERTED A WHERE ID=A.ID AND STATUS = 0
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