码迷,mamicode.com
首页 > 数据库 > 详细

sqlserver 触发器

时间:2015-01-15 00:22:44      阅读:224      评论:0      收藏:0      [点我收藏+]

标签:


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

sqlserver 触发器

标签:

原文地址:http://www.cnblogs.com/li-xy/p/4225079.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!