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. 在iOS APP中使用H5显示百度地图时如何支持HTTPS?

    现象: 公司正在开发一个iOSAPP,使用h5显示百度地图,但是发现同样的H5页面,在安卓可以显示出来,在iOS中就显示不出来. 原因分析: 但是现在iOS开发中,苹果已经要求在APP中的所有对外连接 ...

  2. C# Memcache分布式缓存简单入门

    什么是Memcache?能做什么? 以下是百度的观点: memcache是一套分布式的高速缓存系统,由LiveJournal的Brad Fitzpatrick开发,但目前被许多网站使用以提升网站的访问 ...

  3. 线段树 poj 1436

    题目大意:给出n条垂直于x轴的线段的数据y1,y2,x,求出有几个三条线段一组的三元组并且他们兩兩能相见的.思路:对y轴建树,将x排序,然后按顺序边询问边擦入,用mark[i][j]表示j往左可以看到 ...

  4. maven-各配置文件详解

    1.setting.xml http://www.cnblogs.com/yangxia-test/p/4409736.html <?xml version="1.0" en ...

  5. Servlet和JSP学习指导与实践(二):Session追踪

    前言: web应用中经常需要对某些有用的信息进行存储或者附加一些信息.本文主要介绍session,即“会话”跟踪的几种不同方式~ ----------------------------4种管理ses ...

  6. WCF服务器证书配置说明-没有能够进行密钥交换的私钥,或者进程可能没有访问私钥的权限

    WCF服务器证书配置说明 1.创建证书: makecert.exe -sr LocalMachine -ss My -a sha1 -n CN=XXX -sky exchange -pe 说明: -s ...

  7. java连接mysql

    Java 连接 MySQL 需要驱动包,最新版下载地址为:http://dev.mysql.com/downloads/connector/j/,解压后得到jar库文件,然后在对应的项目中导入该库文件 ...

  8. Bzoj1597 [Usaco2008 Mar]土地购买

    Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 4005  Solved: 1460 Description 农夫John准备扩大他的农场,他正在考虑N ...

  9. js 日期时间排序 数组

    不多说直接show代码 var timeArr=[ {'id':'A01','date':'2016-04-20 23:22:11'}, {'id':'A02','date':'2016-04-21 ...

  10. <<< Tomcat运行报错IOException while loading persisted sessions: java.io.EOFException

    解决方法:将work下面的文件清空,主要是*.ser文件,或者只是删除掉session.ser即可以解决.