CREATE TRIGGER [update_ICMO_gongshi] --自定义触发器的名称
ON [dbo].[ICMO] --生产任务单 此处为表名,触发器的表名,意为在哪个表创建触发
FOR INSERT,UPDATE --触发类型:INSERT、UPDATE、DELETE;分别意为插入时、更新时、删除时触发,依据英文意思理解即可
AS
SET NOCOUNT ON
DECLARE @FInterID int; --声明变量:FInterID 生产任务单内码
DECLARE @FRoutingID int; --声明变量:FRoutingID 工艺内码
select @FInterID = FInterID, @FRoutingID = FRoutingID from Inserted --取当前插入或更新单据工艺内码赋值给@FRoutingID
BEGIN
/*更新生产任务单工时*/
update ICMO --更新生产任务单工时
set FHeadSelfJ01108 = c.f1
from ICMO a --生产任务单
join (select FInterID, sum(FPersonStdTimeRun) f1 --表头内码,汇总标准人工运行工时f1
from t_RoutingOper --工艺路线表体
where FInterID = @FRoutingID --声明变量:FRoutingID 工艺内码
group by FInterID --表头内码
)c
on a.FRoutingID = c.FInterID --生产任务.工艺内码 = c.表头内码
and a.FInterID = @FInterID --生产任务内码单内码
and FStatus in ('0', '5'); --单据状态为计划和确认;
END
select *
from ICMO a --生产任务单
--join t_RoutingOper b --工艺路线表体
--on a.FInterID = @FInterID
--and a.FRoutingID = b.FInterID --生产任务单 = 工艺路线表体.表头内码
join (select FInterID, sum(FPersonStdTimeRun) f1 --表头内码,汇总标准人工运行工时f1
from t_RoutingOper --工艺路线表体
group by FInterID --表头内码
)c
on c.FInterID = '48973'
and a.FInterID = c.FInterID --工艺路线表体.表头内码 = c.表头内码
and FStatus in ('0', '5'); --单据状态为计划和确认
drop trigger [update_ICMO_gongshi]