SQL表新增触发(触发器)
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表新增触发(触发器)的更多相关文章
- 如果公司里有上百个表要做触发器,如果手动写代码的话。很累,所以今天写了一个小程序,自动生成mysql的触发代码。
<?php $dbname = 'test';//数据库 $tab1 = 'user'; //执行的表 $tab2 = 'user_bak'; //被触发的表 $conn = mysql_con ...
- 本表触发更新modifytime,跨表更新modifytime 触发器
一.每行有改动,则触发更新modifytime SQL> create table test(id int, name varchar(10), crdate date, udate date) ...
- SQL中Inserted 和Deleted表 以及触发Trigger
什么是Inserted 和Deleted表 他们有什么用 trigger 的简单实用 1.什么是Inserted 和Deleted表 当插入数据的时候,其实是同时向目的表 和inserted表中插入数 ...
- Sql Server数据库之触发器
阅读目录 一:触发器的优点 二:触发器的作用 三:触发器的分类 四:触发器的工作原理 五:创建触发器 六:管理触发器 概念: 触发器(trigger)是SQL server 提供给程序员和数据分析 ...
- Sql Server系列:触发器
触发器的一些常见用途: ◊ 强制参照完整性 ◊ 常见审计跟踪(Audit Trails):这意味着写出的记录不仅跟踪大多数当前的数据,还包括对每个记录进行实际修改的历史数据. ◊ 创建与CHECK约束 ...
- SQL Server 中的触发器(trigger)
SQL Server 触发器 触发器是一种特殊类型的存储过程,它不同于之前的我们介绍的存储过程.触发器主要是通过事件进行触发被自动调用执行的.而存储过程可以通过存储过程的名称被调用. Ø 什么是触发器 ...
- SQL server与Oracle触发器的创建与使用
SQL Server 1创建触发器 GO BEGIN IF (object_id('WMY', 'tr') is not null) DROP trigger WMY END; GO CREATE T ...
- SQL Server 存储过程、触发器、游标
存储过程 1.存储过程是事先编好的.存储在数据库中的程序,这些程序用来完成对数据库的指定操作. 2.系统存储过程: SQL Server本身提供了一些存储过程,用于管理有关数据库和用户的信息. 用户存 ...
- 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. ...
随机推荐
- 直接启动tomcat时为tomcat指定JDK
第一种: 在windows环境下以批处理文件方式启动tomcat,只要运行<CATALINA_HOME>/bin/startup.bat这个文件,就可以启动Tomcat.在启动时,star ...
- bzoj1800[Ahoi2009]fly 飞行棋 暴力枚举
找了道bzoj的水题,千年难得一遇. 建议初学者做做,然而我个蒟蒻初学时应该A不了..... < http://www.lydsy.com/JudgeOnline/problem.php?id= ...
- # 2015-2016-2 《Java程序设计》课程总结
2015-2016-2 <Java程序设计>课程总结
- Windows phone应用开发[20]-禁止Pivot手势
相比Ios 和Android. Windows Phone 控件库中多了两个比较特殊的空间Pivot 枢轴和Panamera 全景视图控件.在基于枢轴控件Pivot中我们经常会碰到一些比较特殊应用场景 ...
- Hibernate批量处理海量数据的方法
本文实例讲述了Hibernate批量处理海量数据的方法.分享给大家供大家参考,具体如下: Hibernate批量处理海量其实从性能上考虑,它是很不可取的,浪费了很大的内存.从它的机制上讲,Hibern ...
- Bzoj1597 [Usaco2008 Mar]土地购买
Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 4005 Solved: 1460 Description 农夫John准备扩大他的农场,他正在考虑N ...
- Derivative of the softmax loss function
Back-propagation in a nerual network with a Softmax classifier, which uses the Softmax function: \[\ ...
- ionic ios发布图标启动也不能正确加载问题
前两天发布ios的时候发现ios安装的图标和启动页的时候不能正确显示,重新发布也不能正确显示,修改方法 在ionic build ios --release之前执行ionic resources即可
- AE开发使用内存图层
AE开发中,有时需要从磁盘中读取一些文件信息如坐标点转为图层并进行分析,此过程并不需要坐标点入库之类的操作,就可以创建一个内存图层解决问题.创建内存图层需要用到InMemoryWorkspaceFac ...
- ajax 提交表单文件上传
<form action="" method="post" enctype="multipart/form-data" id=&quo ...