SQL表新增触发(触发器)

  1 ALTER TRIGGER [InsertStoreJITOnloadQuantity] ON [dbo].[Sourceing] 
  2 After INSERT
  3 AS
  4 --登記計劃數量(新增時YN=0)
  5 Declare @ID int
  6 Declare @MaterielCode varchar(50)
  7 Declare @Typeofrefno varchar(50)
  8 Declare @PlanQuantity float
  9 Declare @NowQuantity float
 10 Set @ID=0
 11 Select @MaterielCode=MaterielCode,@Typeofrefno=Typeofrefno,@NowQuantity=Quantity from inserted where yn='0' and typeofrefno<>'物料申請單'
 12 IF @Typeofrefno<>'物料申請單' and @MaterielCode<>'' and @MaterielCode is not null
 13 BEGIN
 14    Set @PlanQuantity=@NowQuantity
 15    Execute UpDateStoreJITQuantity 'Plan',@MaterielCode,'倉庫','','',@PlanQuantity,0,0,0 
 16 END
 17 --登記到車頭薄
 18 Declare @Refno varchar(20)
 19 Declare @Date varchar(20)
 20 Declare @Team varchar(20)
 21 Declare @Routing varchar(50)
 22 Declare @JobNo varchar(50)
 23 Declare @FileNo varchar(20)
 24 Declare @FullName varchar(254)
 25 Declare @Account varchar(10)
 26 Declare @ItemNo varchar(80)
 27 Declare @ClientSort varchar(10)
 28 Declare @EndTime varchar(20)
 29 Declare @Deliverydate varchar(20)
 30 Declare @UsedTime float
 31 Declare @Model varchar(100)
 32 Declare @Spectification varchar(254)
 33 Declare @WorkShop varchar(50)
 34 Declare @ThroughID int
 35 Declare @RefrenceID int
 36 Declare @ThroughtPutID int
 37 Declare @MaterielName varchar(254)
 38 Declare @Attribute varchar(20)
 39 Declare @Quantity float
 40 Declare @WBS varchar(20)
 41 Declare @PoDeMID varchar(100)
 42 Declare @MPSNo varchar(200)
 43 Declare @DeliveryAdd varchar(100)
 44 Declare @Account_P Varchar(20)
 45 Declare @Provide varchar(100)
 46 Declare @Country varchar(20)
 47 Declare @ConName varchar(20)
 48 Declare @SenderTel varchar(50)
 49 Declare @PackMeans varchar(100)
 50 Declare @CreationName varchar(20)
 51 Declare @SenderName varchar(20) 
 52 Set @MPSNo=''
 53 Set @ThroughID=0
 54 Set @RefrenceID=0
 55 Set @ThroughtPutID=0
 56 Select @Refno=rtrim(isnull(Refno,'')),@Typeofrefno=isnull(Typeofrefno,''),@MaterielCode=rtrim(isnull(MaterielCode,0)),@MaterielName=isnull(MaterielName,''),@Spectification=isnull(Spectification,''),
 57        @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,''),
 58        @FullName=isnull(FullName,''),@EndTime=convert(varchar(20),TragetRetrunTime,111),@Routing=rtrim(isnull(TypeofWork,'')),@Account=isnull(Account,''),@ItemNo=isnull(ItemNo,''),@ID=ID,@PoDeMID=isnull(PoDeMID,''),
 59        @PackMeans=isnull(PackMeans,''),@Attribute=isnull(Attribute,''),@CreationName=isnull(CreationName,'') from inserted
 60 Set @Team=''
 61 IF @Typeofrefno='訂購單' or @Typeofrefno='訂箱單' 
 62    Begin 
 63     Set @Team='外購'
 64     Set @Routing='備料'
 65    End
 66 IF @Typeofrefno='外發加工單'
 67     Set @Team='外發'
 68 IF @Typeofrefno='客供物品收貨單'
 69    Begin 
 70     Set @Team='客供'
 71     Set @Routing='客供'
 72    End   
 73 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),
 74        @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
 75 IF len(@Team)>0
 76  Begin
 77   Set @UsedTime=datediff(hour,@Date,@EndTime)
 78   IF cast(@UsedTime as float)<0
 79      Set @UsedTime=8 
 80   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
 81   IF len(@JobNo)>0
 82     Begin 
 83       Select @RefrenceID=id from T_Throughput where jobno=@JobNo and Routing=@Routing and Refrenceid=0 and ID=TraceID
 84       --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='外發'
 85     End 
 86   IF @RefrenceID is null or @Team='外發' or @Typeofrefno='訂箱單'
 87    Set @RefrenceID=0
 88   Select @ThroughtPutID=id from T_Throughput where Refno=@Refno and TypeofRefno=@Typeofrefno and MaterielCode=@MaterielCode-- and TargetCompleteDate=@EndTime
 89   IF @ThroughtPutID=0 or @ThroughtPutID is null
 90     BEGIN
 91       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='工作單')
 92     END
 93 --新增外發加工單時增加車頭簿
 94 
 95   IF @ThroughtPutID=0 or @ThroughtPutID is null
 96    Insert Into T_Throughput(Refno,TypeofRefno,MaterielCode,MaterielName,Spectification,Model,Quantity,ClientSort,JobNo,wbs,CreationName,StartTime,EndTime,UsedTime,StartTime1,EndTime1,UsedTime1,FactStartTime,Routing,FileNo,
 97                 FullName,Account,ItemNo,TJob3ID,ThroughID,Team,Device,TypeOfWork,TypeOfWorkNameList,WorkerNoList,WorkShop,RefrenceID,Categories,TargetCompleteDate,Deliverydate,TraceID,Attribute,DynamicGroup) 
 98        values (@Refno,@TypeofRefno,@MaterielCode,@MaterielName,@Spectification,@Model,@Quantity,@ClientSort,@JobNo,@WBS,User_Name(),@Date,@EndTime,@UsedTime,@Date,@EndTime,@UsedTime,@Date,
 99     @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+'.生產') 
100 
101   ELSE
102 
103     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,
104           Refno=@Refno,TypeofRefno=@TypeofRefno,MaterielCode=@MaterielCode,ClientSort=@ClientSort,fact='1',StartTime=@Date,EndTime=@EndTime,UsedTime=@UsedTime,WorkShop=@WorkShop,Team=@Team+@WorkShop,Device=@Team+@WorkShop where id=@ThroughtPutID
105 
106  End
107 
108  --入閘單(上貨單)
109  IF (@Typeofrefno='訂購單' or @Typeofrefno='外發加工單' or @Typeofrefno='客供物品收貨單')and (UPPER(@Account_P) not in('CHX','JDP','AC','WF0-XS','WH','FAT'))
110   Begin
111     Set @ThroughtPutID=0
112     Select @ThroughtPutID=id from Transportation where deliverygoodsno=@Refno and TypeofRefno=@Typeofrefno and FullName=@MaterielName 
113     IF @ThroughtPutID=0 or @ThroughtPutID is null -- 插入
114       IF @CreationName in(select distinct workerno from T_employee)
115         Begin--本地產生入閘單
116           IF @Typeofrefno='訂購單' or @Typeofrefno='外發加工單'
117             Set @ClientSort='入閘單'
118           Else 
119             Set @ClientSort='運輸通知單'
120           If @Typeofrefno='訂購單' or @Typeofrefno='客供物品收貨單' ---有運輸總表資料(外發無)
121             begin
122               Set @ThroughtPutID=0
123               Select @ThroughtPutID=id from TransportationDetial where Refno=@Refno and TypeofRefno=@ClientSort  and Attribute=@TypeOfRefno
124               IF @ThroughtPutID=0 or @ThroughtPutID is null
125                 Begin   
126                   Insert Into TransportationDetial(Refno,OutTime,ByPassWay,DeliverygoodsNo,Account,Creationtime,CreationName,Yn,TotalQuantity,Destination,RefnoP,TypeofRefno,Attribute,SenderTel,SenderName) values (@Refno,@EndTime,@Country,
127                                                    @RefNo,@Account_P,GetDate(),User_Name(),0,@Quantity,@DeliveryAdd,@RefNo,@ClientSort,@TypeOfRefno,@SenderTel,@SenderName)   
128                   Set @ThroughtPutID=@@identity
129                 End  
130               ELSE
131                 Begin    
132                   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
133                 End
134             end
135           If @ThroughtPutID=0
136             Set @ThroughtPutID=Null  
137           --子表
138           Insert into Transportation(ClientSort,deliverygoodsno,typeofrefno,fullname,packmeans,quantity,creationtime,creationname,toarea,destination,account,DeliveryAccount,CompanyName,occurid,OutTime,PID,WBS) values(
139                                      @ClientSort,@Refno,@Typeofrefno,@MaterielName,@PackMeans,@Quantity,getdate(),User_Name(),@Country,@DeliveryAdd,@Account,@Account_P,@Provide,@ID,@EndTime,@ThroughtPutID,@WBS)
140         End
141       Else--遠端產生上貨單
142         Begin
143           --子表
144           Insert into Transportation(ClientSort,deliverygoodsno,typeofrefno,fullname,packmeans,quantity,creationtime,creationname,toarea,destination,account,DeliveryAccount,CompanyName,occurid,OutTime,WBS) values(
145                                      '上貨單',@Refno,@Typeofrefno,@MaterielName,@PackMeans,@Quantity,getdate(),User_Name(),@Country,@DeliveryAdd,@Account,@Account_P,@Provide,@ID,@EndTime,@WBS) 
146         End        
147     Else -- 修改
148       IF @CreationName in(select distinct workerno from T_employee)
149         Begin
150           IF @Typeofrefno='訂購單' or @Typeofrefno='外發加工單'
151             Set @ClientSort='入閘單'
152           Else 
153             Set @ClientSort='運輸通知單'
154           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
155                  and Refno=@Refno and Attribute=@TypeOfRefno
156           Update Transportation set ClientSort=@ClientSort,fullname=@MaterielName,packmeans=@Quantity,quantity=isnull(Quantity,0)+@Quantity,edittime=getdate(),editname=User_Name(),toarea=@Country,OutTime=@EndTime,
157                  destination=@DeliveryAdd,account=@Account,DeliveryAccount=@Account_P,CompanyName=@Provide where id=@ThroughtPutID
158         End
159       Else
160         Begin
161           Update Transportation set ClientSort='上貨單',fullname=@MaterielName,packmeans=@Quantity,quantity=isnull(Quantity,0)+@Quantity,edittime=getdate(),editname=User_Name(),toarea=@Country,OutTime=@EndTime,
162                  destination=@DeliveryAdd,account=@Account,DeliveryAccount=@Account_P,CompanyName=@Provide where id=@ThroughtPutID
163         End
164   End
原文地址:https://www.cnblogs.com/slmdr9/p/5422566.html