日期:2014-05-17 浏览次数:20479 次
ALTER TRIGGER [dbo].[Update] ON [dbo].[t_HTPS] FOR Update,insert AS begin DECLARE @FNO nvarchar(10) --顺序号 DECLARE @FComboBox1 nvarchar(2)--订单类型英文简写 DECLARE @FComboBox1Del nvarchar(2)--删除前的订单类型英文简写 DECLARE @FStartdate DateTime --开始时间 DECLARE @FEnddate DateTime --结束时间 DECLARE @Fdate DateTime --单据时间 DECLARE @FdateDel DateTime --删除前的单据时间 DECLARE @Fid Int --单据内码 --取删除前的代号和删除前的日期 select @FComboBox1Del=FComboBox1, @FdateDel=Fdate from deleted --取更新前的单据内码、订单类型、当月开始和结束时间 select @Fdate=Fdate,@Fid=fid,@FComboBox1=FComboBox1, @FStartdate= case when datepart(day,Fdate)>=26 then dateadd(day,-(datepart(day,Fdate)-26),Fdate) when datepart(day,Fdate)<26 then dateadd(day,26-datepart(day,dateadd(month,-1,Fdate)),dateadd(month,-1,Fdate)) end, @FEnddate= case when datepart(day,Fdate)>=26 then dateadd(day,-(datepart(day,dateadd(month,1,Fdate))-25),Fdate) when datepart(day,Fdate)<26 then dateadd(day,25-datepart(day,Fdate),Fdate) end from inserted --取顺序号 set @FNO=(select top 1 convert(varchar,REPLICATE(0,3-len(right(PSBillNo,3)+1))) + convert(varchar,right(PSBillNo,3)+1) From T_htps where FComboBox1=@FComboBox1 and Fdate between @FStartdate and @FEnddate order by PSBillNo desc) --如果本月没有生成过此类订单的顺序号,那么开始顺序号为001 if @FNO is null or @FNO='' begin set @FNO='001'; end --更新 if @FComboBox1!=@FComboBox1Del or @Fdate!=@FdateDel begin update T_htps set PSBillNo=convert(varchar,@FComboBox1)+convert(varchar,substring(convert(char(12),@FEnddate,112),3,2))+convert(varchar,substring(convert(char(12),@FEnddate,112),5,2))+convert(varchar,@FNO) where fid=@fid and PSBillNo!='' and (FComboBox1!=@FComboBox1Del or Fdate!=@FdateDel) end else if @FComboBox1!=@FComboBox1Del and @Fdate=@FdateDel begin update T_htps set PSBillNo=convert(varchar,@FComboBox1)+convert(varchar,substring(convert(char(12),@FEnddate,112),3,2))+convert(varchar,substring(convert(char(12),@FEnddate,112),5,2))+convert(varchar,@FNO) where fid=@fid and PSBillNo='' end end