|
发表于 2019-8-1 12:59:54
|
显示全部楼层
可以参照这个触发器示例:
CREATE TRIGGER [dbo].[DispatchLists_INSERT]
ON [dbo].[DispatchLists]
AFTER INSERT,UPDATE
AS
declare
@string Varchar(200),
@DLID varchar(50),
@cCusCode varchar(50),
@cInvCode varchar(50),
@cBatch varchar(50),
@cVenCode varchar(50),
@cVenName varchar(50),
@cout1 int,
@cout2 int
BEGIN
select @DLID=DLID,@cCusCode=cDefine33,@cInvCode=cInvCode,@cBatch=cBatch,@cVenName=cbatchproperty6,@cVenCode=cbatchproperty7 from inserted
select @cout1=count(*) from dbo.V_EK_CUS_CINV_VEN where cCusCode=@cCusCode and cInvCode=@cInvCode
select @cout2=count(*) from dbo.V_EK_CUS_CINV_VEN where cCusCode=@cCusCode and cInvCode=@cInvCode and cVenCode=@cVenCode
-- 是否控制产品
IF @cout1>0
BEGIN
-- 插入触发
IF EXISTS(SELECT 1 FROM inserted) AND NOT EXISTS(SELECT 1 FROM deleted)
BEGIN
if @cout2 <1
begin
SET @string=char(10)+'产品('+convert(varchar, @cInvCode)+')对应批次号('+convert(varchar, @cBatch)+')的供货单位('+convert(varchar, @cVenName)+')在客户BOM中不存在,请重新指定供货单位后保存!'
RAISERROR (@string,16,1)
end
END
-- 更新触发
IF EXISTS(SELECT 1 FROM inserted) AND EXISTS(SELECT 1 FROM deleted)
BEGIN
if @cout2 <1
begin
SET @string=char(10)+'产品('+convert(varchar, @cInvCode)+')对应批次号('+convert(varchar, @cBatch)+')的供货单位('+convert(varchar, @cVenName)+')在客户BOM中不存在,请重新指定供货单位后保存!'
RAISERROR (@string,16,1)
end
END
END
END
GO
|
|