--创建一张日志表
create table TriggerLog(
logid int,--更新的主键
fid int,--物料id
fnumber varchar(255), --分组编码
stockname varchar(255), --库房名称
fdate date --更新日期
)
alter trigger Update_material_stock on t_BD_MaterialStock after
insert
as
declare @id as int --定义变量
declare @fid as int --定义物料id,作为日志插入日志表中
declare @fdate as date --定义当前时间,插入日志
select @id=inserted.FENTRYID from inserted --变量赋值 新增时的主键赋值给id
select @fid=inserted.FMATERIALID from inserted --将新增的物料的id赋值
select @fdate=convert(varchar,getdate(),120)
--分组开头为1001,1002,1008
if exists (select c.FNUMBER,* from t_BD_MaterialStock a left join T_BD_MATERIAL c on a.FMATERIALID=c.FMATERIALID left join T_BD_MATERIALGROUP b on b.fid=c.fmaterialgroup where (b.FNUMBER like '1001%' or b.FNUMBER like '1002%'or b.FNUMBER like '1008%'or b.FNUMBER like '2003%') and a.FENTRYID=@id and a.FMATERIALID=@fid)
--更新为板材仓
begin update t_BD_MaterialStock set FSTOCKID=114763 where fentryid=@id and FMATERIALID=@fid --主键作为过滤条件
insert into TriggerLog values (@id,@fid,'1001or1002or1008or2003','板材仓',@fdate);
end
---最后新增一条物料,然后查询日志表,对比数据。
推荐阅读