sqlserver 触发器


CREATE TRIGGER IC_Deploy_SNP_TRIGGER
ON IC_Deploy
FOR Insert,Update
AS
BEGIN
DECLARE @LocalOrgCode CHAR(3);
DECLARE @OutOrgCode char(3);
DECLARE @InOrgCode char(3);
DECLARE @OrderType SMALLINT;
DECLARE @SPARE2 int;
IF EXISTS(SELECT 1 FROM inserted) AND NOT EXISTS(SELECT 1 FROM deleted)
begin
select @LocalOrgCode=value FROM t_SysParaDic WHERE para=1;
select @OutOrgCode=deployoutstation from inserted;
select @InOrgCode=deployinstation from inserted;
select @OrderType=ordertype from inserted;

IF (@OrderType=3 or @OrderType=4) AND @LocalOrgCode=@OutOrgCode
BEGIN
INSERT INTO IC_Deploy_SNP_ ( type, wasteSN,DeployOutCenter,DeployOutStation,DeployOutIP,DeployInCenter,DeployInStation,DeployInIP,PlanCount,OptCount,RealCount,SendOrderTime,SendOrderOper,SendOrderOrg,SendOrderOrgType,OrderStatus,OrderType,ChangeOrderTime,ChangeOrderOper,ChangeOrderOrg,ChangeOrderOrgType,spare1,spare2,spare3, INSERTTIME ) select ‘I‘ type, wasteSN,DeployOutCenter,DeployOutStation,DeployOutIP,DeployInCenter,DeployInStation,DeployInIP,PlanCount,OptCount,RealCount,SendOrderTime,SendOrderOper,SendOrderOrg,SendOrderOrgType,OrderStatus,OrderType,ChangeOrderTime,ChangeOrderOper,ChangeOrderOrg,ChangeOrderOrgType,spare1,spare2,spare3, GETDATE() INSERTTIME from inserted;
END
end
ELSE IF EXISTS(SELECT 1 FROM inserted) AND EXISTS(SELECT 1 FROM deleted)
begin
select @LocalOrgCode=value FROM t_SysParaDic WHERE para=1;
select @OutOrgCode=deployoutstation from inserted;
select @InOrgCode=deployinstation from inserted;
select @OrderType=ordertype from inserted;
select @SPARE2=SPARE2 from inserted;

if update(wasteSN) --更新的字段是主键
begin
--增加删除记录
INSERT INTO IC_Deploy_SNP_ ( type, wasteSN,DeployOutCenter,DeployOutStation,DeployOutIP,DeployInCenter,DeployInStation,DeployInIP,PlanCount,OptCount,RealCount,SendOrderTime,SendOrderOper,SendOrderOrg,SendOrderOrgType,OrderStatus,OrderType,ChangeOrderTime,ChangeOrderOper,ChangeOrderOrg,ChangeOrderOrgType,spare1,spare2,spare3, INSERTTIME ) select ‘D‘ type, wasteSN,DeployOutCenter,DeployOutStation,DeployOutIP,DeployInCenter,DeployInStation,DeployInIP,PlanCount,OptCount,RealCount,SendOrderTime,SendOrderOper,SendOrderOrg,SendOrderOrgType,OrderStatus,OrderType,ChangeOrderTime,ChangeOrderOper,ChangeOrderOrg,ChangeOrderOrgType,spare1,spare2,spare3, GETDATE() INSERTTIME from deleted;
--增加新增记录
INSERT INTO IC_Deploy_SNP_ ( type, wasteSN,DeployOutCenter,DeployOutStation,DeployOutIP,DeployInCenter,DeployInStation,DeployInIP,PlanCount,OptCount,RealCount,SendOrderTime,SendOrderOper,SendOrderOrg,SendOrderOrgType,OrderStatus,OrderType,ChangeOrderTime,ChangeOrderOper,ChangeOrderOrg,ChangeOrderOrgType,spare1,spare2,spare3, INSERTTIME ) select ‘I‘ type, wasteSN,DeployOutCenter,DeployOutStation,DeployOutIP,DeployInCenter,DeployInStation,DeployInIP,PlanCount,OptCount,RealCount,SendOrderTime,SendOrderOper,SendOrderOrg,SendOrderOrgType,OrderStatus,OrderType,ChangeOrderTime,ChangeOrderOper,ChangeOrderOrg,ChangeOrderOrgType,spare1,spare2,spare3, DATEADD(ss,1,GETDATE()) INSERTTIME from inserted;
end
else --更新的字不是主键
begin
IF @SPARE2=1 and (@OrderType=1 OR @OrderType=3) and @LocalOrgCode=@OutOrgCode
BEGIN
INSERT INTO IC_Deploy_SNP_ ( type, wasteSN,DeployOutCenter,DeployOutStation,DeployOutIP,DeployInCenter,DeployInStation,DeployInIP,PlanCount,OptCount,RealCount,SendOrderTime,SendOrderOper,SendOrderOrg,SendOrderOrgType,OrderStatus,OrderType,ChangeOrderTime,ChangeOrderOper,ChangeOrderOrg,ChangeOrderOrgType,spare1,spare2,spare3, INSERTTIME ) select ‘U‘ type, wasteSN,DeployOutCenter,DeployOutStation,DeployOutIP,DeployInCenter,DeployInStation,DeployInIP,PlanCount,OptCount,RealCount,SendOrderTime,SendOrderOper,SendOrderOrg,SendOrderOrgType,OrderStatus,OrderType,ChangeOrderTime,ChangeOrderOper,ChangeOrderOrg,ChangeOrderOrgType,spare1,spare2,spare3, GETDATE() INSERTTIME from inserted;
end
ELSE IF @SPARE2=1 and (@OrderType=2 OR @OrderType=4) and @LocalOrgCode=@InOrgCode
BEGIN
INSERT INTO IC_Deploy_SNP_ ( type, wasteSN,DeployOutCenter,DeployOutStation,DeployOutIP,DeployInCenter,DeployInStation,DeployInIP,PlanCount,OptCount,RealCount,SendOrderTime,SendOrderOper,SendOrderOrg,SendOrderOrgType,OrderStatus,OrderType,ChangeOrderTime,ChangeOrderOper,ChangeOrderOrg,ChangeOrderOrgType,spare1,spare2,spare3, INSERTTIME ) select ‘U‘ type, wasteSN,DeployOutCenter,DeployOutStation,DeployOutIP,DeployInCenter,DeployInStation,DeployInIP,PlanCount,OptCount,RealCount,SendOrderTime,SendOrderOper,SendOrderOrg,SendOrderOrgType,OrderStatus,OrderType,ChangeOrderTime,ChangeOrderOper,ChangeOrderOrg,ChangeOrderOrgType,spare1,spare2,spare3, GETDATE() INSERTTIME from inserted;
end
ELSE IF @SPARE2=2 and @OrderType=3 and @LocalOrgCode=@OutOrgCode
BEGIN
INSERT INTO IC_Deploy_SNP_ ( type, wasteSN,DeployOutCenter,DeployOutStation,DeployOutIP,DeployInCenter,DeployInStation,DeployInIP,PlanCount,OptCount,RealCount,SendOrderTime,SendOrderOper,SendOrderOrg,SendOrderOrgType,OrderStatus,OrderType,ChangeOrderTime,ChangeOrderOper,ChangeOrderOrg,ChangeOrderOrgType,spare1,spare2,spare3, INSERTTIME ) select ‘U‘ type, wasteSN,DeployOutCenter,DeployOutStation,DeployOutIP,DeployInCenter,DeployInStation,DeployInIP,PlanCount,OptCount,RealCount,SendOrderTime,SendOrderOper,SendOrderOrg,SendOrderOrgType,OrderStatus,OrderType,ChangeOrderTime,ChangeOrderOper,ChangeOrderOrg,ChangeOrderOrgType,spare1,spare2,spare3, GETDATE() INSERTTIME from inserted;
end
ELSE IF @SPARE2=2 and @OrderType=4 and @LocalOrgCode=@InOrgCode
BEGIN
INSERT INTO IC_Deploy_SNP_ ( type, wasteSN,DeployOutCenter,DeployOutStation,DeployOutIP,DeployInCenter,DeployInStation,DeployInIP,PlanCount,OptCount,RealCount,SendOrderTime,SendOrderOper,SendOrderOrg,SendOrderOrgType,OrderStatus,OrderType,ChangeOrderTime,ChangeOrderOper,ChangeOrderOrg,ChangeOrderOrgType,spare1,spare2,spare3, INSERTTIME ) select ‘U‘ type, wasteSN,DeployOutCenter,DeployOutStation,DeployOutIP,DeployInCenter,DeployInStation,DeployInIP,PlanCount,OptCount,RealCount,SendOrderTime,SendOrderOper,SendOrderOrg,SendOrderOrgType,OrderStatus,OrderType,ChangeOrderTime,ChangeOrderOper,ChangeOrderOrg,ChangeOrderOrgType,spare1,spare2,spare3, GETDATE() INSERTTIME from inserted;
end

end
end
END
GO

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。