小白求SQL 触发器:现有A,B两表,要求B表新增或更改时,A表中的PRICE 字段更新到B中的PRICE ,为空不更新

2024-12-28 23:57:28
推荐回答(2个)
回答1:

create TRIGGER B_U
ON dbo.B
for insert UPDATE
AS
if exists(select price from inserted where price is not null)
update dbo.B
set PRICE
from inserted,dbo.A
where inserted.ID=dbo.B.ID and inserted.NAME =dbo.A.NAME

回答2:

create TRIGGER B_U
ON dbo.B
for insert UPDATE
AS
update dbo.B
set PRICE
from inserted,dbo.A
where inserted.ID=dbo.B.ID and inserted.NAME =dbo.A.NAME