ALTER TRIGGER [InsertStoreJITOnloadQuantity] ON [dbo].[Sourceing]
After INSERT
AS
--登記計劃數量(新增時YN=0)
Declare @ID int
Declare @MaterielCode varchar(50)
Declare @Typeofrefno varchar(50)
Declare @PlanQuantity float
Declare @NowQuantity float
Set @ID=0
Select @MaterielCode=MaterielCode,@Typeofrefno=Typeofrefno,@NowQuantity=Quantity from inserted where yn='' and typeofrefno<>'物料申請單'
IF @Typeofrefno<>'物料申請單' and @MaterielCode<>'' and @MaterielCode is not null
BEGIN
Set @PlanQuantity=@NowQuantity
Execute UpDateStoreJITQuantity 'Plan',@MaterielCode,'倉庫','','',@PlanQuantity,0,0,0
END
--登記到車頭薄
Declare @Refno varchar(20)
Declare @Date varchar(20)
Declare @Team varchar(20)
Declare @Routing varchar(50)
Declare @JobNo varchar(50)
Declare @FileNo varchar(20)
Declare @FullName varchar(254)
Declare @Account varchar(10)
Declare @ItemNo varchar(80)
Declare @ClientSort varchar(10)
Declare @EndTime varchar(20)
Declare @Deliverydate varchar(20)
Declare @UsedTime float
Declare @Model varchar(100)
Declare @Spectification varchar(254)
Declare @WorkShop varchar(50)
Declare @ThroughID int
Declare @RefrenceID int
Declare @ThroughtPutID int
Declare @MaterielName varchar(254)
Declare @Attribute varchar(20)
Declare @Quantity float
Declare @WBS varchar(20)
Declare @PoDeMID varchar(100)
Declare @MPSNo varchar(200)
Declare @DeliveryAdd varchar(100)
Declare @Account_P Varchar(20)
Declare @Provide varchar(100)
Declare @Country varchar(20)
Declare @ConName varchar(20)
Declare @SenderTel varchar(50)
Declare @PackMeans varchar(100)
Declare @CreationName varchar(20)
Declare @SenderName varchar(20)
Set @MPSNo=''
Set @ThroughID=0
Set @RefrenceID=0
Set @ThroughtPutID=0
Select @Refno=rtrim(isnull(Refno,'')),@Typeofrefno=isnull(Typeofrefno,''),@MaterielCode=rtrim(isnull(MaterielCode,0)),@MaterielName=isnull(MaterielName,''),@Spectification=isnull(Spectification,''),
@Model=isnull(Model,''),@Quantity=isnull(Quantity,0),@ClientSort=isnull(ClientSort,''),@JobNo=rtrim(isnull(JobNo,'')),@WBS=isnull(WBS,''),@Date=convert(varchar(20),Creationtime ,111 ),@FileNo=isnull(FileNo,''),
@FullName=isnull(FullName,''),@EndTime=convert(varchar(20),TragetRetrunTime,111),@Routing=rtrim(isnull(TypeofWork,'')),@Account=isnull(Account,''),@ItemNo=isnull(ItemNo,''),@ID=ID,@PoDeMID=isnull(PoDeMID,''),
@PackMeans=isnull(PackMeans,''),@Attribute=isnull(Attribute,''),@CreationName=isnull(CreationName,'') from inserted
Set @Team=''
IF @Typeofrefno='訂購單' or @Typeofrefno='訂箱單'
Begin
Set @Team='外購'
Set @Routing='備料'
End
IF @Typeofrefno='外發加工單'
Set @Team='外發'
IF @Typeofrefno='客供物品收貨單'
Begin
Set @Team='客供'
Set @Routing='客供'
End
Select @WorkShop=isnull(Account,''),@ThroughID=ID,@EndTime=case when (@EndTime is null or len(@EndTime)<=0 or datediff(hour,@Date,@EndTime)<=0) then convert(varchar(20),TragetRetrunTime,111) else @EndTime end,@DeliveryAdd=rtrim(DeliveryAdd),
@Account_P=rtrim(Account),@Provide=rtrim(Provide),@Country=Country,@ConName=ConName,@SenderTel=Provi_tele,@SenderName=Conname from Source_main where Refno=@Refno and TypeOfRefno=@Typeofrefno
IF len(@Team)>0
Begin
Set @UsedTime=datediff(hour,@Date,@EndTime)
IF cast(@UsedTime as float)<0
Set @UsedTime=8
Select @MPSNo=case when len(@MPSNo)>0 then (case when charindex(rtrim(MPSNo),@MPSNo)<=0 then @MPSNo+','+rtrim(MPSNo) else @MPSNo end) else MPSNo end,@Deliverydate=convert(varchar(20),CDeliveryDate ,111 ) from Po_list where charindex(convert(varchar(20),id),@PoDeMID)>0
IF len(@JobNo)>0
Begin
Select @RefrenceID=id from T_Throughput where jobno=@JobNo and Routing=@Routing and Refrenceid=0 and ID=TraceID
--UpDate T_Throughput set Quantity=isnull(Quantity,0)+@Quantity,fact='1',StartTime=@Date,EndTime=@EndTime,UsedTime=@UsedTime,WorkShop=@WorkShop from T_Throughput where jobno=@JobNo and Routing=@Routing and DynamicGroup='外發'
End
IF @RefrenceID is null or @Team='外發' or @Typeofrefno='訂箱單'
Set @RefrenceID=0
Select @ThroughtPutID=id from T_Throughput where Refno=@Refno and TypeofRefno=@Typeofrefno and MaterielCode=@MaterielCode-- and TargetCompleteDate=@EndTime
IF @ThroughtPutID=0 or @ThroughtPutID is null
BEGIN
Select @ThroughtPutID=id from T_Throughput where Refno=@Refno and TypeofRefno=@Typeofrefno and MaterielCode=@MaterielCode and JobNo=@Jobno and Routing=@Routing and Refrenceid=0 and ID=TraceID and len(rtrim(isnull(JobNo,'')))>0 and (ClientSort='工程單' or ClientSort='工作單')
END
--新增外發加工單時增加車頭簿 IF @ThroughtPutID=0 or @ThroughtPutID is null
Insert Into T_Throughput(Refno,TypeofRefno,MaterielCode,MaterielName,Spectification,Model,Quantity,ClientSort,JobNo,wbs,CreationName,StartTime,EndTime,UsedTime,StartTime1,EndTime1,UsedTime1,FactStartTime,Routing,FileNo,
FullName,Account,ItemNo,TJob3ID,ThroughID,Team,Device,TypeOfWork,TypeOfWorkNameList,WorkerNoList,WorkShop,RefrenceID,Categories,TargetCompleteDate,Deliverydate,TraceID,Attribute,DynamicGroup)
values (@Refno,@TypeofRefno,@MaterielCode,@MaterielName,@Spectification,@Model,@Quantity,@ClientSort,@JobNo,@WBS,User_Name(),@Date,@EndTime,@UsedTime,@Date,@EndTime,@UsedTime,@Date,
@Routing,@FileNo,@MaterielName,@Account, @ItemNo,@ID,@ThroughID,@Team+@WorkShop,@Team+@WorkShop,@Team+@WorkShop,@Routing,User_Name(),@WorkShop,@RefrenceID,@MPSNo,@EndTime,@Deliverydate,@RefrenceID,@Attribute,@Team+@WorkShop+'.生產') ELSE Update T_Throughput set Quantity=isnull(Quantity,0)+@Quantity,TargetCompleteDate=@EndTime,categories=case when charindex(@MPSNo,rtrim(isnull(categories,'')))>0 then rtrim(isnull(Categories,'')) else (case when len(rtrim(isnull(Categories,'')))>0 then @MPSNo +','+rtrim(isnull(Categories,'')) else @MPSNo end) end,
Refno=@Refno,TypeofRefno=@TypeofRefno,MaterielCode=@MaterielCode,ClientSort=@ClientSort,fact='',StartTime=@Date,EndTime=@EndTime,UsedTime=@UsedTime,WorkShop=@WorkShop,Team=@Team+@WorkShop,Device=@Team+@WorkShop where id=@ThroughtPutID End --入閘單(上貨單)
IF (@Typeofrefno='訂購單' or @Typeofrefno='外發加工單' or @Typeofrefno='客供物品收貨單')and (UPPER(@Account_P) not in('CHX','JDP','AC','WF0-XS','WH','FAT'))
Begin
Set @ThroughtPutID=0
Select @ThroughtPutID=id from Transportation where deliverygoodsno=@Refno and TypeofRefno=@Typeofrefno and FullName=@MaterielName
IF @ThroughtPutID=0 or @ThroughtPutID is null -- 插入
IF @CreationName in(select distinct workerno from T_employee)
Begin--本地產生入閘單
IF @Typeofrefno='訂購單' or @Typeofrefno='外發加工單'
Set @ClientSort='入閘單'
Else
Set @ClientSort='運輸通知單'
If @Typeofrefno='訂購單' or @Typeofrefno='客供物品收貨單' ---有運輸總表資料(外發無)
begin
Set @ThroughtPutID=0
Select @ThroughtPutID=id from TransportationDetial where Refno=@Refno and TypeofRefno=@ClientSort and Attribute=@TypeOfRefno
IF @ThroughtPutID=0 or @ThroughtPutID is null
Begin
Insert Into TransportationDetial(Refno,OutTime,ByPassWay,DeliverygoodsNo,Account,Creationtime,CreationName,Yn,TotalQuantity,Destination,RefnoP,TypeofRefno,Attribute,SenderTel,SenderName) values (@Refno,@EndTime,@Country,
@RefNo,@Account_P,GetDate(),User_Name(),0,@Quantity,@DeliveryAdd,@RefNo,@ClientSort,@TypeOfRefno,@SenderTel,@SenderName)
Set @ThroughtPutID=@@identity
End
ELSE
Begin
Update TransportationDetial set OutTime=@EndTime,ByPassWay=@Country,DeliverygoodsNo=@Refno,Account=@Account_P,Edittime=Getdate(),EditName=User_Name(),TotalQuantity=isnull(TotalQuantity,0)+@Quantity,Destination=@DeliveryAdd,RefnoP=@Refno,SenderTel=@SenderTel,Sendername=@Sendername where id=@ThroughtPutID
End
end
If @ThroughtPutID=0
Set @ThroughtPutID=Null
--子表
Insert into Transportation(ClientSort,deliverygoodsno,typeofrefno,fullname,packmeans,quantity,creationtime,creationname,toarea,destination,account,DeliveryAccount,CompanyName,occurid,OutTime,PID,WBS) values(
@ClientSort,@Refno,@Typeofrefno,@MaterielName,@PackMeans,@Quantity,getdate(),User_Name(),@Country,@DeliveryAdd,@Account,@Account_P,@Provide,@ID,@EndTime,@ThroughtPutID,@WBS)
End
Else--遠端產生上貨單
Begin
--子表
Insert into Transportation(ClientSort,deliverygoodsno,typeofrefno,fullname,packmeans,quantity,creationtime,creationname,toarea,destination,account,DeliveryAccount,CompanyName,occurid,OutTime,WBS) values(
'上貨單',@Refno,@Typeofrefno,@MaterielName,@PackMeans,@Quantity,getdate(),User_Name(),@Country,@DeliveryAdd,@Account,@Account_P,@Provide,@ID,@EndTime,@WBS)
End
Else -- 修改
IF @CreationName in(select distinct workerno from T_employee)
Begin
IF @Typeofrefno='訂購單' or @Typeofrefno='外發加工單'
Set @ClientSort='入閘單'
Else
Set @ClientSort='運輸通知單'
Update TransportationDetial set OutTime=@EndTime,ByPassWay=@Country,DeliverygoodsNo=@Refno,Account=@Account_P,Edittime=Getdate(),EditName=User_Name(),TotalQuantity=isnull(TotalQuantity,0)+@Quantity,Destination=@DeliveryAdd,RefnoP=@Refno where TypeofRefno=@ClientSort
and Refno=@Refno and Attribute=@TypeOfRefno
Update Transportation set ClientSort=@ClientSort,fullname=@MaterielName,packmeans=@Quantity,quantity=isnull(Quantity,0)+@Quantity,edittime=getdate(),editname=User_Name(),toarea=@Country,OutTime=@EndTime,
destination=@DeliveryAdd,account=@Account,DeliveryAccount=@Account_P,CompanyName=@Provide where id=@ThroughtPutID
End
Else
Begin
Update Transportation set ClientSort='上貨單',fullname=@MaterielName,packmeans=@Quantity,quantity=isnull(Quantity,0)+@Quantity,edittime=getdate(),editname=User_Name(),toarea=@Country,OutTime=@EndTime,
destination=@DeliveryAdd,account=@Account,DeliveryAccount=@Account_P,CompanyName=@Provide where id=@ThroughtPutID
End
End

SQL表新增触发(触发器)的更多相关文章

  1. 如果公司里有上百个表要做触发器,如果手动写代码的话。很累,所以今天写了一个小程序,自动生成mysql的触发代码。

    <?php $dbname = 'test';//数据库 $tab1 = 'user'; //执行的表 $tab2 = 'user_bak'; //被触发的表 $conn = mysql_con ...

  2. 本表触发更新modifytime,跨表更新modifytime 触发器

    一.每行有改动,则触发更新modifytime SQL> create table test(id int, name varchar(10), crdate date, udate date) ...

  3. SQL中Inserted 和Deleted表 以及触发Trigger

    什么是Inserted 和Deleted表 他们有什么用 trigger 的简单实用 1.什么是Inserted 和Deleted表 当插入数据的时候,其实是同时向目的表 和inserted表中插入数 ...

  4. Sql Server数据库之触发器

    阅读目录 一:触发器的优点 二:触发器的作用 三:触发器的分类 四:触发器的工作原理 五:创建触发器 六:管理触发器 概念:   触发器(trigger)是SQL server 提供给程序员和数据分析 ...

  5. Sql Server系列:触发器

    触发器的一些常见用途: ◊ 强制参照完整性 ◊ 常见审计跟踪(Audit Trails):这意味着写出的记录不仅跟踪大多数当前的数据,还包括对每个记录进行实际修改的历史数据. ◊ 创建与CHECK约束 ...

  6. SQL Server 中的触发器(trigger)

    SQL Server 触发器 触发器是一种特殊类型的存储过程,它不同于之前的我们介绍的存储过程.触发器主要是通过事件进行触发被自动调用执行的.而存储过程可以通过存储过程的名称被调用. Ø 什么是触发器 ...

  7. SQL server与Oracle触发器的创建与使用

    SQL Server 1创建触发器 GO BEGIN IF (object_id('WMY', 'tr') is not null) DROP trigger WMY END; GO CREATE T ...

  8. SQL Server 存储过程、触发器、游标

    存储过程 1.存储过程是事先编好的.存储在数据库中的程序,这些程序用来完成对数据库的指定操作. 2.系统存储过程: SQL Server本身提供了一些存储过程,用于管理有关数据库和用户的信息. 用户存 ...

  9. ORACLE PL/SQL编程:把触发器说透

    本篇主要内容如下: 8.1 触发器类型 8.1.1 DML触发器 8.1.2 替代触发器 8.1.3 系统触发器 8.2 创建触发器 8.2.1 触发器触发次序 8.2.2 创建DML触发器 8.2. ...

随机推荐

  1. 重构alert,confirm

    最近写了一个重构的alert,confirm控件,调用时直接使用alert,confirm即可 //调用方法 alert("提示语") window.confirm('你确定要删除 ...

  2. 聊聊 Web 项目二维码生成的最佳姿势

    在设计和实现的过程之后,你永远不知道部署上去的程序会已什么样的姿势运行. 本篇借一次生成二维码逻辑的不同实现,阐述 Web 项目中二维码生成的正确姿势. 文中如有批量,欢迎各位看客老爷拍砖.试运行前5 ...

  3. try catch中用了 Response.Redirect 引发的线程异常终止

    记录一下,提醒自己. 今天写代码的时候,在try 中写了一句  Response.Redirect 在 catch 把页面重定向到了另外一个地方 本来是想打算,如果没出现异常,就定到页面A,如果异常了 ...

  4. 微信快速开发框架(七)--发送客服信息,版本更新至V2.2 代码已更新至github

    在V2版本发布的博文中,已经介绍了大多数Api的用法,同时也收到了很多意见,其中发布了几个修正版本,修改了几个bug,在此感谢大家的使用,有了大家的支持,相信快速开发框架会越来越好,也会越来越完善的. ...

  5. python设计模式

    本节内容 设计模式介绍 设计模式分类 设计模式6大原则 1.设计模式介绍 设计模式(Design Patterns) --可复用面向对象软件的基础 设计模式(Design pattern)是一套被反复 ...

  6. time & datetime

    时间相关的操作,时间有三种表示方式: 时间戳               1970年1月1日之后的秒,即:time.time() 格式化的字符串    2014-11-11 11:11,    即:t ...

  7. 设计模式 “续”

    观察者模式 再次申明,本文学习自 程杰 兄的 "大话设计模式" 根据书中的内容和自己的体会而来. 观察者模式也叫 发布.订阅模式,在这个模式里,一个目标物件管理所有依赖它的观察者物 ...

  8. linux下压缩,解压缩的方法

    linux zip命令 zip -r myfile.zip ./* 将当前目录下的所有文件和文件夹全部压缩成myfile.zip文件,-r表示递归压缩子目录下所有文件. 2.unzip unzip - ...

  9. sqlalchemy入门记录

    前言 发现翻译全文时间比较久,所以先整个简单的使用说明吧 安装SQLAlchemy pip install sqlalchemy 查看SQLAlchemy版本 In [1]: import sqlal ...

  10. Spring-IOC-BeanFactory

    BeanFactory BeanFactory 是 Spring 的“心脏”.它就是 Spring IoC 容器的真面目.Spring 使用 BeanFactory 来实例化.配置和管理 Bean.但 ...