求SQL删除触发器,

来源:百度知道 编辑:UC知道 时间:2024/05/09 05:07:22
有表A和表B
表A字段如下:
APrdCode
APrdWeight

表B字段如下:
BPrdCode
BPrdWeight

我想在表A中删除一条记录后
将表B中BprdCode=AprdCode,的BprdWeight值进行更新(Bprdweight=BprdWeight-AprdWeight)。
请问应该如何写这个触发器?
谢谢!
set @Acode =select APrdCode form from deleted --从逻辑表deleted中读取A中删除的APrdCode
set @Aw =select form APrdCode from deleted --从逻辑表deleted中读取A中删除的记录的APrdWeight
set @Bw =select form APrdCode from B where BPrdCode=@Acode --B中BPrdWeight
update table B set BprdCode=@Bw - @Aw where BPrdCode=@Acode --删除后B的BprdCode

在查询分析时以上代码会有以下提示。
在关键字 'select' 附近有语法错误。
在关键字 'select' 附近有语法错误。
在关键字 'select' 附近有语法错误。
在关键字 'table' 附近有语法错误。

请问:set @Aw =select form APrdCode from deleted 中的form是什么意思呢?
多谢!

不好意思刚才写错了

create TRIGGER temp1
on A for delete --删除时触发
as
DECLARE @Acode nvarchar(20)--声明变量用于记录删除的APrdCode
DECLARE @Aw numeric --声明变量用于记录删除的记录的APrdWeight
DECLARE @Bw numeric --声明变量用于B中BPrdWeight
select @Acode = APrdCode from deleted --从逻辑表deleted中读取A中删除的APrdCode
select @Aw =APrdWeight from deleted --从逻辑表deleted中读取A中删除的记录的APrdWeight
select @Bw =BPrdWeight from B where BPrdCode=@Acode --B中BPrdWeight
update B set BprdCode=@Bw - @Aw where BPrdCode=@Acode --删除后B的BprdCode

--deleted 和 inserted 是逻辑(概念)表。这些表在结构上类似于定义触发器的表(也就是在其中尝试用户操作的表);这些表用于保存用户操作可能更改的行的旧值或新值。